leetcode---Nim Game---博弈
2016-04-04 13:58
525 查看
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?
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?
class Solution { public: bool canWinNim(int n) { return n % 4 != 0; } };
相关文章推荐
- Linux ALSA声卡驱动之四:Control设备的创建
- unityShader笔记 1 之 边缘色
- 在Handler的post方法调用Toast
- 库文件
- Linux下date命令,格式化输出,时间设置
- 宏和函数的区别
- Linux ALSA声卡驱动之三:PCM设备的创建
- 心形线以及其对心脏的模拟
- RNN-RBM for music composition 网络架构及程序解读
- 或许是 Nginx 上配置 HTTP2 最实在的教程了
- 字符串哈希之散列表处理冲突 poj1880
- Sublime Es6教程2-基本语法
- Bayesian statistics
- Linux ALSA声卡驱动之二:声卡的创建
- C#中的索引举例
- 蘑菇街2016校园招聘——聊天
- boost::any小结
- 深入理解Scala的隐式转换系统
- 使用Git向Github提交代码
- 循环神经网络(RNN, Recurrent Neural Networks)介绍