leetcode 292. Nim Game
来源:互联网 发布:迅龙恢复软件注册码 编辑:程序博客网 时间:2024/06/03 11:41
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.
这一题就是,发现了其中的数学奥妙的人一行就解决了,妄想用程序做出来的人,就stackoverflow了。。。我原来想着用递归,结果栈溢出了。好吧,看看他们的理论。基础情况: 当
n = 4
时,无论first player拿走几个石子,second player都会拿走剩下的石子,输。当
1* 4 < n < 2 * 4, (n = 5, 6, 7)
, first player可以在第一次拿的时候,做到留给second player 4个石子,所以赢。所以5,6,7就是winning numbers ,当一个玩家得到winning numbers时,他就能赢。现在开始下一个循环,
n = 8
, 无论first player拿几个石子, 都会让second player拿到winning numbers (5, 6, 7) . 所以, 8 % 4 == 0, 又是一个死亡数字.对于在8~12之间的数字,
9, 10, 11
, 都是对first player来说的 winning numbers , 因为first player能做到留给second player死亡数字8.
public boolean canWinNim(int n) { return n % 4 != 0 ;}
- [leetcode] 292.Nim Game
- 【LeetCode】292.Nim Game
- leetcode:292. Nim Game
- leetCode 292. Nim Game
- 292. Nim Game LeetCode
- LeetCode 292. Nim Game
- LeetCode 292. Nim Game
- LeetCode 292.Nim Game
- LeetCode 292. Nim Game
- [LeetCode]292.Nim Game
- <LeetCode> 292. Nim Game
- leetcode-292.Nim Game
- LeetCode 292. Nim Game
- LeetCode *** 292. Nim Game
- leetcode 292. Nim Game
- Leetcode 292. Nim Game
- 292.[LeetCode]Nim Game
- 【leetcode】292. Nim Game
- python杂记
- jmeter插件之自定义场景图(万能场景设计)
- 什么是启发式算法(heuristic algorithm)?
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
- Kubernetes1.6中配置私有 DNS 区域以及上级域名服务
- leetcode 292. Nim Game
- Windows Error 错误代码全解
- 支持定义前后缀文字和文字颜色的TextView
- chatr.js 制作漂亮的图表
- vue环境搭建vue-cli,vue-router,webpack
- Jetty的安装、配置和使用
- 裁剪网格纸
- 用户注册时,Yii利用AJAX页面实时提示用户名已经存在
- C++十字链表实现有向图