292. Nim Game
2016-07-09 22:30
281 查看
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.
Hint:
If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
dp[i]=(!dp[i-1])&(!dp[i-2])&(!dp[i-3])
更简洁的就是n能不能被4整除。
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.
Hint:
If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
T | T | T | F | T | T | T | F |
更简洁的就是n能不能被4整除。
public boolean canWinNim(int n) { return !(n%4==0); }
相关文章推荐
- Gym 100463A Crossing Number(树状数组)
- 共享onload事件
- POJ 1442 Treap模板
- GridView中BoundField类型列的改行方法
- 【Linux】阻塞信号
- Java 相关
- SSH(三)
- Masonry介绍与使用实践(快速上手Autolayout)
- leetcode.373. Find K Pairs with Smallest Sums
- MapReduce原理
- viewpager引导页
- sqlite3学习笔记——sqlite3中日期时间的自动增量
- 配置Struts2(以登录为例)
- 290. Word Pattern
- 技术布道者宣言
- zabbix_server
- nyoj35 表达式求值
- windows 下配置 Nginx 常见问题
- Bzoj2440 完全平方数
- hdu 5062(水题)