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。

  1. class Solution {
  2. public:
  3. bool canWinNim(int n) {
  4. if(n % 4 ==0)
  5. return false;
  6. else
  7. return true;
  8. }
  9. };

  

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. pointers on c (day 1,chapter1)

    c语言的优先级 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 () 圆括号 (表达式)/函数名(形参表) . 成员选择(对象) 对象.成员名 ...

  2. spring aop两种配置方式(1)

    第一种:注解配置AOP注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before ...

  3. 【bzoj2333】 [SCOI2011]棘手的操作 可并堆+lazy标记

    2016-05-31  21:45:41 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 (学习了黄学长的代码 有如下操作: U x y ...

  4. 51nod算法马拉松13

    A 取余最长路 不难发现路径可以拆成三条线段,只要知道两个转折点的位置就能计算出答案. 设sum(i,l,r)表示第i行从l到r元素的和,则答案可以表示为sum(1,1,x)+sum(2,x,y)+s ...

  5. 找到一个Flex中LineChart很好的学习博客

    http://blog.flexexamples.com/category/linechart/ 里面链接复制的时候失效了,请直接点击原页面进行查看 Setting specific minimum ...

  6. FMS发布视频流H.264如何设置

    FMS这个话题由来已久,H.264这个编码格式也由来已久.FMS不叫FMS了,改叫AMS了.因为是Adobe. 今天就说说flash发布流媒体视频,以H.264编码出现的问题.在网上找,大把的关于as ...

  7. Linux软件安装为什么名字不一样

    一.说安装 1.安装 yacc :# yum install byacc 2.安装 glib: :# yum install glibc 3.安装 wireshark :# yum install w ...

  8. osgearth_city例子总结

    osgearth_city例子总结 转自:http://blog.csdn.net/taor1/article/details/8242480 int main(int argc, char** ar ...

  9. C#中Post和Get提交

    1.Post提交 private string PostWebRequest(string Url, string paramData, string dataEncode) { string ret ...

  10. 纯css3绘制扇形

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...