LeetCode——Nim Game

Description:

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个,取得最后一个石头的人赢。给一个整数n判断是不是能赢。

这个问题其实是一个著名的博弈论的问题,但是这个地方有点简单,列出来前12个答案就能找出规律,就是只要是4的倍数都不可能赢。

Nim Game:http://www.guokr.com/blog/777525/   http://www.cnblogs.com/exponent/articles/2141477.html

代码:

public class Solution {
    public boolean canWinNim(int n) {
        return !(n%4==0);
    }
}
时间: 2024-10-23 02:00:11

LeetCode——Nim Game的相关文章

[LeetCode] Nim Game 尼姆游戏

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 th

LeetCode Nim Game (简单nim)

题意: 有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家.你先取,问最后谁赢? 思路: n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数,而对方最多只能取到3个,剩下的给先手来取,所以先手赢. 1 class Solution { 2 public: 3 bool canWinNim(int n) { 4 return n%4>0; 5 } 6 }; AC代码

LeetCode No.1 (Nim name)

LeetCode  Algorithum # title solution difficulty 292 Nim Name java easy         NO.292 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 w

【一天一道LeetCode】#292. Nim Game.md

一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

LeetCode 第 292 题 (Nim Game)

LeetCode 第 292 题 (Nim Game) 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

一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)

最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim Game): 桌子上有一堆石块,你和你的朋友轮流去拿这些石块,每次只能拿1块.2块或者3块.在石块被拿光前,最后一次拿到石块的人获胜.你将首先去拿这些石块. 你和你的朋友都非常聪明,并且拥有应对该游戏的最佳策略.写一个函数来决定在给定石块数量的情况下,你是否能够获胜.比如:如果桌子上有4块石块,那么

算法 - leetcode 292 Nim Game

算法 - leetcode 292 Nim Game  一丶题目 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏. 输入: 4 输出: false 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛: 因为无论你拿走 1 块.2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走. 二丶思路 1)

【算法功底】LeetCode 292 Nim Game

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 th

[leetcode] 292. Nim Game 解题报告

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 th