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. c# socket编程简单例子

    服务器端代码 using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Thr ...

  2. FIREDAC直连ORACLE数据库

    UniDac对Oracle的Direct连接,不需要套Oracle客户端dll,deploy时真的时 方便又快捷.FireDac连接Oracle,在没有Oracle Client的情况下,是可以连接上 ...

  3. Linux下c/c++项目代码覆盖率的产生方法

    最近做了一系列的单元测试相关的工作,除了各种规范及测试框架以外,讨论比较多的就是关于代码覆盖率的产生,c/c++与其他的一些高级语言或者脚本语言相比较而言,例如 Java..Net和php/pytho ...

  4. Cloud Computing Deployment Models

    Cloud computing can broadly be broken down into three main categories based on the deployment model. ...

  5. 【不积跬步,无以致千里】关闭631端口cups打印服务和8009端口ajp

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. javascript keycode大全【转载】

    keycode    8 = BackSpace BackSpace keycode    9 = Tab Tabkeycode   12 = Clearkeycode   13 = Enterkey ...

  7. LFI漏洞利用总结(转载)

    主要涉及到的函数include(),require().include_once(),require_once()magic_quotes_gpc().allow_url_fopen().allow_ ...

  8. Js Pattern - Self Define Function

    This pattern is useful when your function has some initial preparatory work to do andit needs to do ...

  9. DOS命令生成文件列表

    DOS命令窗口,生成文件列表命令格式:dir xmlFiles /b >list.txt dir 文件列表相关命令 xmlFiles 待生成文件所在文件夹,在dos命令窗口根目录下,省略前缀.别 ...

  10. 工作vs.学�

    近一两年来,我先后对[工作与学习]的复杂过程有过多次的头脑风暴,而且感觉在这方面略有所成(看这里和这里):当然既然仅仅是头脑风暴,所谓的所成也仅仅是一些粗糙的想法,一些没有实证过的如果,算是积累而已, ...