这题其实不太好想,用到了博弈论

1,2,3 能赢

4 输

5,6,7 赢

8 输

9,10,11 赢

12 输

那么结论就是4的倍数就是输,其他情况就能赢。

为什么会是这样呢?很好解释,根源就在4会输,那么5,6,7就会凑到4,让对方输,那对方也会这么想,8的时候无论你拿1,2,3块对方都会凑4

如果是9,10,11,就拿1,2,3凑8,依次推论,只要第一次拿的时候凑到4的整数就行,下面对方拿多少还是凑到4的整数,最后到4对方还是要输。

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

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

    Description: You are playing the following Nim Game with your friend: There is a heap of stones on t ...

  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. Js中的window.parent ,window.top,window.self 详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  2. 注解@PostConstruct与@PreDestroy讲解及实例

    从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的vo ...

  3. 9x9乘法表输出[Java]

    笔试,9x9乘法表输出的问题,看似简单,回来把当时写的结果输入一遍后发现,并没有想象中的“完美”.把改写的pos在此,警示...不要忘记任何细节. public class NXN { public ...

  4. java代码实现打包多个文件下载功能

    //传入对应的需要打包的file 集合对象 //文件打包下载     public static HttpServletResponse downLoadFiles(List<File> ...

  5. Logistic Regression vs Decision Trees vs SVM: Part II

    This is the 2nd part of the series. Read the first part here: Logistic Regression Vs Decision Trees ...

  6. ArcGIS Server 10.1 错误 service failed to start,

    启动发布的地图服务时出现如下错误: ERROR: service failed to start, ServiceStarter thread timeout. 具体原因未知. Google中说了可能 ...

  7. log4net 配置应用

    (一). WinForm 或者 WPF 中的配置和应用 <?xml version="1.0" encoding="utf-8" ?> <co ...

  8. windows自带的压缩,解压缩命令

    压缩一个文件: makecab c:\ls.exe ls.zip 解压一个文件: expand c:\ls.zip c:\ls.exe

  9. imac上php环境php+apache+mysql

    ---恢复内容开始--- Mac OS X系统已预装集成了Apache+php,但是在新的系统中苹果取消了图形界面,所以只能从命令行开启了. 启用apache: 打开终端 在终端中可以查看集成的php ...

  10. python 字符串与数字之间的转换

    1.数字转字符串 i = 123 str = ‘%d’ %i str即为转换成的字符串 2.字符串转换成数字: import string tt='555' ts=string.atoi(tt) ts ...