Nim Game

原创 2016年05月31日 15:06:03

开始刷起了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.

思路:当这个人轮到4的时候必输,轮到5的时候自己拿一个,把4轮给另一方,则另一方必输,于是每逢4的倍数的时候都是必输。

public class Solution {
public boolean canWinNim(int n) {
    if(n%4==0)
        return false;
    return true;      
    }

}

相关文章推荐

LeetCode从零单排之一分段——Nim Game(尼姆博弈)

原题(你们这里洋文好的人很多啊,我就不瞎翻译了): You are playing the following Nim Game with your friend: There is a h...
  • dt_yee
  • dt_yee
  • 2015年11月24日 12:35
  • 434

[SPOJ IGAME Interesting Game]Nim 博弈+数位DP

[SPOJ IGAME Interesting Game]Nim 博弈+数位DP分类:数学 逆元 水题1. 题目链接[SPOJ IGAME Interesting Game]2. 题意描述Alice和...

leetcode submission/20161004(nim game)

讲真我个人不是很喜欢coding,因为对于算法这一类玩意儿总是记不住,平时靠感觉写东西的时候比较多,再加上前段时间忙着找房子,买房子(坐标北京),耽误了点儿时间,赶上国庆才抽空写点儿,不过还是希望养成...

LeetCode Nim Game

题目: You are playing the following Nim Game with your friend: There is a heap of stones on the t...

zoj3529 A Game Between Alice and Bob 质因子个数NIM

A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and B...

292. Nim Game [easy] (Python)

题目链接https://leetcode.com/problems/nim-game/题目原文 You are playing the following Nim Game with your f...

Fibonacci again and again (尼姆博弈 nim game)

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...

LeetCode笔记:292.Nim Game

问题: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl...

Codeforces Round #188 (Div. 1) Game with Powers 引发的联想 NIM游戏 Sprague Grundy Theory

考完试,有空总结一下一个很大的收获:Sprague Grundy Theory 这是由Codeforces Round #188 (Div. 1) Game with Powers这道题引发的思考,一...

LeetCode 292. Nim Game 题解(C++)

You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Nim Game
举报原因:
原因补充:

(最多只允许输入30个字)