###### tags: `Leetcode` `easy` `math` `python` `c++` # 292. Nim Game ## [題目連結:] https://leetcode.com/problems/nim-game/description/ ## 題目: ![](https://i.imgur.com/dsrRx4o.png) ![](https://i.imgur.com/TVKHCzD.png) ## 解題想法: * 此題目為給n個石頭,你與對手每次可以拿走1~3顆 * 最先拿完的人獲勝 * 你先開始拿 * 判斷是否你能獲勝 * 基本的if else判斷是否為4的倍數即可 ## Python: ``` python= class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: bool """ if n<4: return True if n%4==0: return False else: return True ``` ## C++: ``` cpp= class Solution { public: bool canWinNim(int n) { if (n<4) return true; if (n%4==0) return false; else return true; } }; ```