292. Nim Game(C++)
292. Nim Game(C++)
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?
题目大意:
解题方法:
这题往小说可以追溯到小学奥数或者脑筋急转弯的书中,往大说可以深究到博弈论。然而编程在这里并没有卵用,策略在于,因为每个人都取不到4个,假设自己后 走,要保证每轮自己和对方取得数量的和是4,这样就能确保每轮完后都有4的倍数个石头被取走。这样,如果我们先走的话,先把n除4的余数个石头拿走,这样 不管怎样,到最后都会留4个下来,对方取1个你就取3个,对方取2个你就取2个,就必赢了。
注意事项:
C++代码:
class Solution
{
public:
bool canWinNim(int n)
{
return n%!=;
}
};
292. Nim Game(C++)的更多相关文章
- 292. Nim Game
292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- 292. Nim游戏
292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- 292. Nim Game - LeetCode
Question 292. Nim Game Solution 思路:试着列举一下,就能发现一个n只要不是4的倍数,就能赢. n 是否能赢 1 true 2 true 3 true 4 false 不 ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
随机推荐
- WordPress ‘get_allowed_mime_types’函数安全漏洞(2)
漏洞名称: WordPress ‘get_allowed_mime_types’函数安全漏洞 CNNVD编号: CNNVD-201309-169 发布时间: 2013-09-13 更新时间: 2013 ...
- Tap.js
Tap.js A lightweight ‘tap’ event JavaScript plugin
- 数学(概率):HNOI2013 游走
[题目描述] 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这 ...
- HDU-2509 Be the Winner
http://acm.hdu.edu.cn/showproblem.php?pid=2509 Be the Winner Time Limit: 2000/1000 MS (Java/Others) ...
- 学习嵌入式Linux有没有一个最佳的顺序(持续更新)
作为一个嵌入式Linux的初学者,我知道我可能将长期处于初学者阶段,因为我至今仍然没有能够摸索出一条很好的道路让我由初学者进入到更高级阶段.但是我始终没有放弃,本篇文章就是用来记录我学习嵌入式Linu ...
- linux常用命令大全(转)
由于记忆力有限,把平时常用的Linux命令整理出来,以便随时查阅: linux 基本命令 ls (list 显示当前目录下文件和目录 ls -l 详细显示 =ll ) [root@linux ...
- C#执行带参数的Oracle存储过程
public void UpdateByRowGuid(string RowGuid) { //OracleConnection conn = new OracleConnection("d ...
- 放爬虫nginx
禁止火狐浏览器访问 例子
- Android之TextureView浅析
近期.在改动Android4.4的原生相机Camera2,非常习惯的去寻找SurfaceView.结果任凭我使用grep还是ack.都无法搜索到SurfaceView,最后还是通过代码CameraAc ...
- centos6.5 安装
导系统,进入rescue模式,在提示符下输入grub,进入grub提示符.(不管用什么方法,能进入grub就行)root(hd?,?) //前一个问号是你第几块硬盘,后一个是LINUX所在的第几个分区 ...