292. Nim Game

Problem

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.

Example

Input: 4
Output: false 
Explanation: 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.

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool canWinNim(int n) {
        return n%4!=0;
    }
};

Result

292. Nim Game.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 14,238评论 3 20
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,126评论 0 10
  • 财商决定财富 作业4,我的想法写出来不怕大家笑话,真正的能反应出穷人为什么是穷人?小市民气息飘满整个宇宙。 “50...
    金源儿阅读 1,488评论 0 0
  • 你看飞机 早上起床,和小妞在床上打闹嬉戏。 小妞突然向天花板一指,说:你看有飞机。 我不由得好笑,在屋里,哪里能看...
    欣然2018阅读 3,060评论 0 0