Nim Game
2015-10-13 15:46
330 查看
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.
题目意思:
现有一个游戏,在一个桌子上放了若干个石头,每次可以移除1到3个石头,谁最后一个移除桌子上的所有石头谁就赢了,你可以第一次移除。
解题如下:
bool canWinNim(int n) {
return (n%4) ? true:false;
}
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.
题目意思:
现有一个游戏,在一个桌子上放了若干个石头,每次可以移除1到3个石头,谁最后一个移除桌子上的所有石头谁就赢了,你可以第一次移除。
解题如下:
bool canWinNim(int n) {
return (n%4) ? true:false;
}
相关文章推荐
- Android面试题库---横竖屏转换
- HttpMime 处理 多部件 POST 请求
- 编码再解码解决乱码问题
- 什么是常识?一个人独立生活所具备的能力
- java的HashSet的介绍
- 运行时加断点 VM disconnected问题
- android:inputType常用取值
- CocoaPods 安装和使用教程
- HttpMime 处理 多部件 POST 请求
- 扣出thinkphp数据库操作类
- gcc 、configure 用法
- C# 操作域用户
- androidUI开发中的规范
- 邮箱输入框测试用例
- jquery.validate验证表单,自己的js提交数据,返回json
- 三层结构复选框勾选控制
- kubernetes集群部署
- 对象的转型
- 贝叶斯MATLAB
- MySQL group_concat_max_len