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;      
    }

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

相关文章推荐

HDU 3389 Game(nim博弈)

Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac...

Leetcode:292. Nim Game

Leetcode题解292.Nim Game-Easy题目

UVA 11859 Division Game (Nim博弈)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32746 题意:有一个n*m(1 分析:考虑每个数包含的素因子个数(...

Division Game UVA - 11859 (Nim)题解

Division Game UVA - 11859 (Nim)题解

Leetcode - Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac...

292. Nim Game

题目:Nim Game原题链接 You are playing the following Nim Game with your friend: There is a heap of sto...

leetCode 292. Nim Game

题目连接:https://leetcode.com/problems/nim-game/     题目内容: You are playing the following Nim Game ...

Nim Game

Nim Game 标签:leetcode,C++

LeetCode 292: Nim Game 题解

别看题这么长,其实很简单。LeetCode 292: Nim Game Problem: You are playing the following Nim Game with your fr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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