Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

Hint:

  1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
 public class Solution {
public boolean canWinNim(int n) {
if(n%4 == 0)
{
return false;
}
else
{
return true;
}
}
}
 /*************************************************************************
> File Name: LeetCode292.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 10 May 2016 07:30:06 PM CST
************************************************************************/ /************************************************************************* Nim Game You are playing the following Nim Game with your friend:
There is a heap of stones on the table,
each time one of you take turns to remove 1 to 3 stones.
The one who removes the last stone will be the winner.
You will take the first turn to remove the stones. Both of you are very clever and have optimal strategies for the game.
Write a function to determine whether you can win the game
given the number of stones in the heap. For example, if there are 4 stones in the heap,
then you will never win the game:
no matter 1, 2, or 3 stones you remove,
the last stone will always be removed by your friend. Hint: If there are 5 stones in the heap,
could you figure out a way to remove the stones
such that you will always be the winner? ************************************************************************/ #include <stdio.h> int canWinNim( int n )
{
int ret = ;
if( n% != )
{
ret = ;
}
return ret;
} int main()
{
int n = ;
int ret = canWinNim(n);
printf("%d\n", ret);
return ;
}

LeetCode 292的更多相关文章

  1. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  2. 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)(转)

    最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...

  3. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  4. 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)

    最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...

  5. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  6. Java实现 LeetCode 292 Nim游戏

    292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...

  7. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  8. Leetcode 292 Nim Game 博弈论

    class Solution {public:    bool canWinNim(int n) {        return n % 4 != 0;    }};

  9. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

随机推荐

  1. 现代程序设计homework-06

    现代程序设计homework-06 1) 把程序编译通过, 跑起来. 加入了倒退的功能,程序已经能跑起来了(见代码). 不过倒退功能有些bug,不过这是由于原本程序的主逻辑就有点问题(对于不可走的格子 ...

  2. LINUX如何查看其他用户的操作

    我们知道可以使用history命令,查看自己的操作记录,但如果你是root用户,如何查看其它用户的操作记录呢?   其实history命令只是把当前用户目录下的~/.bash_History文件内容列 ...

  3. Java IO (4) - Writer

    Java IO (4) - Writer 前言 JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包.Java 流在处理上分为字符流 ...

  4. Git使用过程中出现项目文件无法签入Source Control的情况

    在VS中使用Git进行项目source control的过程中,有些文件不在source control之下,右键点击时,也找不到Undo, Commit命令 无法把他们签入进Source Contr ...

  5. OpenCDN2.0安装

    部署说明 为网站加速,建立私有的CDN节点群,每部署一个CDN节点只需5分钟,无节点数量上限!参考 http://ocdn.me/ 安装需求 OpenCDN的Beta版目前在CentOS5.x - C ...

  6. Unity3D之移植学习笔记:移植到Android平台更好的方法

    接上文,之前我们采用了直接将Unity项目导出为Eclipse项目来修改的方式,这种做法存在的一个最大的问题就是:每当Unity被修改之后,都需要重新导出,而每次导出的项目在Android平台方面的J ...

  7. C#学习笔记(五):泛型

    认识泛型 泛型使类型参数化,从而实现了算法上的代码重用. 同时由于去掉了转换中装箱和拆箱的操作,使用泛型还可以提高程序的运行速度. 我们先看看C#自带的使用了泛型的类: using System.Co ...

  8. ASP防注入

    因为在改进公司的一套ASP代码,所以考虑了一下防注入的问题. 参考了网上的几处代码,进行了修改和整合,都转换成小写再处理. 还考虑了script注入. 代码如下: 'Asp防注入代码 SQL_injd ...

  9. 搜狐cache文件夹设置

    比如说本来sohucache放在E盘,你想改到D盘.第一步:在运行中打开regedit,在弹出的HKEY_CURRENT_USER选中Software第二步:再选中SOHU再选中SoHuVA再选中Ca ...

  10. IE自动化

    自动打开www.baidu.com,并搜索关键字“abcd” #定义函数:等待页面打开 function WaitForLoad($ie) { sleep 2 do {Sleep -Mi 200} w ...