码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode -- Nim Game

时间:2015-10-31 11:37:01      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:


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.


一个拿石子的游戏,有n个石子,每次可以从中拿走[1,3]个。你和另一个人轮流拿石子,你先拿。
问:对于数量为n的石子,能否判断你必胜?




思路:
如果有4个石子,轮到谁谁就输了;
有8个石子,轮到谁谁也输,因为我无论拿几个,对方都会有办法给我剩4个;
以此类推,有4k个石子,轮到谁谁输。


而由于我先拿,因此只要第一次我能保证给对方剩余4的倍数个石子,我就能赢。
这样就等于把这个拿石子问题转化为了判断n是否为4的倍数的问题。


因此代码很简单:




 public bool CanWinNim(int n) {
        if(n < 4){
    		return true;
    	}
    	
    	return n % 4 != 0;
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode -- Nim Game

标签:

原文地址:http://blog.csdn.net/lan_liang/article/details/49531079

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!