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 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.

My Solution

1. 第一种直觉的想法是,在给定的石头个数基础上,A拿一次,B拿一次,这样计算所有的排列组合。然后立即觉得这个想法太蠢,而且计算这种排列组合的代码也不大会写,遂放弃。

2. 想起Add Digits这道题目的解法,那么不妨写出开始的几种情况试试。

令石头个数为K,从K=1开始考虑。每次均为A先取。

前三种情况:K=1,2,3的时候,A一次取完,故A胜。

K=4时,A无论取多少,取完之后K都在1-3之间,落回到前三种情况,此时相当于K=1~3而B先取的情况,故B一定胜。

K=5时,A只要取完之后,令K的值为【若A先取则B一定胜】的情况下的值,在此例中K为4,则此时的情况就相当于K=4且B先取,则A一定获胜。

同理,K=6时,令A取完之后K=4;K=7时,令A取完之后K=4,则A一定能获胜。

当K=8时,A无论怎样取,都只能使得K落在5、6、7之间,此时相当于,K=5~7且B先取,先取者胜,所以A无论如何无法取胜。

同理,K=12时,A无法使K在取一次后变成8,此时A也无法取胜。

故,每当K=4n(n为正整数)时,A无法取一次后将K变为【若A先取则B一定胜】的情况。此时B如果足够聪明,一定是可以稳操胜券的。

代码如下:

bool canWinNim(int n) {
    return n % 4 != 0;
}

话说,我总做这些AC率这么高的题目,怎么不觉着羞愧呢。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值