Description:

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.

有一堆石头,两个人轮流取,一次只能取1~3个,取得最后一个石头的人赢。给一个整数n判断是不是能赢。

这个问题其实是一个著名的博弈论的问题,但是这个地方有点简单,列出来前12个答案就能找出规律,就是只要是4的倍数都不可能赢。

Nim Game:http://www.guokr.com/blog/777525/   http://www.cnblogs.com/exponent/articles/2141477.html

代码:

public class Solution {
public boolean canWinNim(int n) {
return !(n%==);
}
}

LeetCode——Nim Game的更多相关文章

  1. [LeetCode] Nim Game 尼姆游戏

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

  2. LeetCode: Nim Game

    这题其实不太好想,用到了博弈论 1,2,3 能赢 4 输 5,6,7 赢 8 输 9,10,11 赢 12 输 那么结论就是4的倍数就是输,其他情况就能赢. 为什么会是这样呢?很好解释,根源就在4会输 ...

  3. LeetCode Nim Game (简单nim)

    题意: 有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家.你先取,问最后谁赢? 思路: n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数, ...

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

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

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

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

  6. 【一天一道LeetCode】#292. Nim Game

    一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...

  7. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

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

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

  9. 【LeetCode】292. Nim Game 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. vs 无法启动iis

    visual studio 2013/2015/2017 有时会碰到iis无法启动, 查看系统事件日志发现,缺少 aspnetcore.dll 文件 . 下载此文件,并复制到缺少的目录中,重启visu ...

  2. perl 安装Image::Magick 模块

    ImageMagick 是一个处理图片的库,有C, perl, python, java 等多种语言对应的库 在安装perl 对应的Image::Magick 模块之前,首先需要安装 ImgeMagi ...

  3. R 语言消除pdf图片的空白

    pdf 函数中,有一个参数onefile ,  默认值为TRUE, 表示每张图片占用pdf 中的一页 当遇到pdf的前几页为空白时,是因为画图的代码产生了几张空白的图片 如果要消除前面的空白,只需要设 ...

  4. VMware克隆虚拟机后无法启动网卡

    最简单的办法: 修改这个文件:/etc/udev/rules.d/70_persistent-net.rules 把eth1修改为eth0,记下里面的mac地址 修改/etc/sysconfig/ne ...

  5. Android学习笔记——log无法输出的解决方法和命令行查看log日志

    本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/JohnTsai/p/3983936.html. 知识 ...

  6. 常用的PostBuild Event 脚本

    参考: http://blog.csdn.net/fengzhijiaxu/article/details/4827364 http://blog.csdn.net/teng_ontheway/art ...

  7. Golang 在 Mac、Linux、Windows 交叉编译

    https://blog.csdn.net/panshiqu/article/details/53788067

  8. C# AES要解密的数据的长度无效

    加密方式  AES-CBC-128 将解密方法改成如下 public string Decrypt(string toDecrypt, string key) { if (string.IsNullO ...

  9. matlab矩阵内存预分配

    matlab矩阵内存预分配就意味着,划定一个固定的内存块,各数据可直接按"行.列指数"存放到对应的元素中.若矩阵中不预配置内存.则随着"行.列指数"的变大.MA ...

  10. iOS 图片加载速度极限优化—FastImageCache解析

    FastImageCache是Path团队开发的一个开源库,用于提升图片的加载和渲染速度,让基于图片的列表滑动起来更顺畅,来看看它是怎么做的.优化点iOS从磁盘加载一张图片,使用UIImageVIew ...