题目:
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.

分析:
题意是2个人玩游戏,桌上有一堆石头,一次只能拿1到3个石头,谁把最后的石头拿走,谁就是赢家。现在,你是第一次拿石头的人。写程序来判断,给定石头数n,你是否可以必赢。

简单来说,撇去第一次拿的石头,无论对方拿几个,你都要有应对的数量。对方出1时,2人组合出的数量是2~4;对方出2时,组合出的数量是3~5;对方出3时,数量是4~6。也就是4这个数字是无论对方出什么,都可以组合出来的。故,我们将每4个石头作为一组。假设总数是4的倍数,那无论你每次拿什么,对方都可以跟你组成4,最终的石头一定是对方拿的;反之,如果不是4的倍数,你只要把除以4的余数,在第一次拿走,从第二次开始,无论对方出什么,都组成4,你就必赢了。

代码实现1:

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

代码实现2:

public class Solution {public boolean canWinNim(int n) {int nk=n&3;return nk != 0;}
}

Leet Code OJ 292. Nim Game [Difficulty: Easy]相关推荐

  1. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  3. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  4. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  5. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

  6. Leet Code OJ 1. Two Sum [Difficulty: Easy]

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. Leet Code OJ 223. Rectangle Area [Difficulty: Easy]

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  8. Leet Code OJ 189. Rotate Array [Difficulty: Easy]

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  9. Leet Code OJ 66. Plus One [Difficulty: Easy]

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

最新文章

  1. MTK6515 android打版软件配置(DrvGen.exe 使用)
  2. 脚本升级_Openssh升级脚本
  3. 在服务器系统怎么设置地址怎么办,路由器怎么设置地址
  4. [渝粤教育] 西南科技大学 中学英语教材教法 在线考试复习资料
  5. fmc是fpga直接引出来的吗_家长速看!你还在用“不要和陌生人说话”糊弄孩子的安全教育吗?...
  6. python将excel表按地方拆分_Python将一个Excel拆分为多个Excel
  7. 解决SecureCRT与SecureFX中文乱码问题
  8. Vertx JDBC 批处理
  9. 【转】Qt中的QString,QByteArray,Qchar, char*
  10. istio使用教程和示例(导流,请求路由,访问拒绝,黑白名单,限速)
  11. python如何屏幕截图_Python编写屏幕截图程序方法
  12. java B2B2C springmvc mybatis电子商城系统-分布式配置中心(Spring Cloud Config)
  13. 汉语词典 mdd mdx 下载_三款你必须拥有的英文词典软件
  14. 格式化U盘的工具(USB无法识别 ,无法格式化 )
  15. 上下文无关文法的组成
  16. python创建简单网站
  17. 达梦DM7数据库服务启动异常,管道文件已存在,DmAPService dead but pid file exists
  18. python100天从新手到大师下载_GitHub - chenqiyi/Python-100-Days: Python - 100天从新手到大师...
  19. TLS SNI(TLS Server Name Indication)配置:F5、Nginx和IIS
  20. 19号笔刷 ps_【精品】PS绘画19号笔刷正确使用方法

热门文章

  1. Kafka设计解析(五): Kafka Consumer设计解析
  2. PostgreSQL COPY 导入/导出数据
  3. GetModuleHandle,AfxGetInstanceHandle使用区别
  4. 搭建 LEGO EV3 的 PyCharm Python 开发环境
  5. python爬取糗事百科
  6. 五分钟快速理解 Reactor 模型
  7. Linux中的基础IO(一)
  8. 音视频技术开发周刊 | 228
  9. Better Video Better Audio 的时代已经过去了吗?
  10. 音视频技术开发周刊 | 133