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.

本题中,由于你是第一个拿石子的人,所以,只要所有的石子数目为4的整数倍,那么就必然会输,因为你的对手可以保证在你拿走一次石子后,始终每次拿走石子的个数和你拿走石子的个数和为4。

class Solution {
public:
bool canWinNim(int n) {
if(n % 4 ==0)
return false;
else
return true;
}
};

  

lleetcode 292. Nim Game的更多相关文章

  1. 292. Nim Game

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

  2. 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 ...

  3. 292. Nim Game(C++)

    292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...

  4. 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) { ...

  5. 【Leetcode】292. Nim Game

    problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...

  6. 292. Nim游戏

    292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...

  7. 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个石子之后都是必胜,则当前必败 ...

  8. Java实现 LeetCode 292 Nim游戏

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

  9. 292. Nim Game - LeetCode

    Question 292. Nim Game Solution 思路:试着列举一下,就能发现一个n只要不是4的倍数,就能赢. n 是否能赢 1 true 2 true 3 true 4 false 不 ...

随机推荐

  1. xml基本操作和保存配置文件应用实例

    引言:在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.本文首先介绍了xml的基本操作,后面写了一个经常用到的xml保存配置文件的实例. xml常用方法: 定义 ...

  2. iOS学习01C语言数据类型

    1.注释 // 单行注释 // 注释对代码起到解释说明的作用,注释是给程序员看的,不参与程序运行 /*  多行注释  Xcode快捷键  全选 cmd+a  复制 cmd+c  粘贴 cmd+v  设 ...

  3. nginx负载均衡之基于客户端cookie的会话保持

    通过ip_hash做会话保持有一定的缺陷,这个是通过客户端ip来实现.同一个网络下众多客户端访问服务器会被扔到同一台机器,再或者是CDN也 会导致负载不均衡.所以要实现通过客户端cookie实现,包括 ...

  4. BZOJ4644 : 经典傻逼题

    设每个点的权值为和它相连的所有边的权值的异或和,那么等价于选若干个点,使得点权异或和最大,这显然只需要维护一组线性基,然后从高位到低位贪心选取即可. 对于本题,因为有修改操作,所以考虑按时间分治,并用 ...

  5. Eclipse Java注释模板设置详解,更改 ${user}和${date}

    修改MyEclipse eclipse 注释的作者名字 转自:http://www.oschina.net/question/158170_31311 在eclipse/myeclipse中,当我们去 ...

  6. HDU 3090 (贪心)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3090 题目大意:一共n段路.每段路每千米都会被抢劫一定数量,可以雇佣武士护卫m千米.问最少被抢劫数量 ...

  7. 移动端开发——javascript

    javascript(简称js)语言在移动前端应用很广.可以说必不可少,许多效果都是和js相关的.包括现在移动端的一些框架.jqmobi.jqtouch.sencha touch.jquerymobi ...

  8. passing ‘const ’ as ‘this’ argument of ‘’ discards qualifiers 错误处理

    示例程序: #include <iostream> #include <set> using   namespace std ; class   StudentT { publ ...

  9. Log打印日志遇到的问题

    Log日志打印出现空指针问题 AndroidRuntime(372): Caused by: java.lang.NullPointerException: println needs a messa ...

  10. Linux_安装软件包

    一.软件包: 源码包 二进制包(rpm包,编译完成) 依赖性 包A-->包B-->包C 一.rpm 挂载镜像,从镜像文件中找到要安装的rpm包 [root@hadoop09-linux ~ ...