1、Nim Game(Easy)

Problem description

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.

Analyze

Code

class Solution {
    var stonesAmount: Int = 0
    
    func canWinNim(n: Int) -> Bool {
        if n % 4 == 0 {
            return false
        } else {
            return true
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,660評論 5 6
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,134評論 0 23
  • 那一年我小學(xué)一年級 我交到了一個朋友 她是我的小前桌 她有一頭長發(fā) 喜歡把頭發(fā)扎起來 我一有功夫就揪她辮子 她讓我...
    孤獨(dú)的路燈閱讀 457評論 1 3
  • 金秋九月,我們劃著一條條航班弧線,從五湖四海集合到長春管院。 綠色的校園,清新的空氣,藍(lán)藍(lán)的天,長春管院以朝氣蓬勃...
    冷眼魯娜閱讀 708評論 0 0
  • 人還是不能活的太過于閉塞,不斷與人結(jié)識相處的過程中你可以不斷的自我提升、認(rèn)識和改正。當(dāng)然,一定的定力和判斷力與改變...
    到底需要多少睡眠多少水閱讀 280評論 0 0

友情鏈接更多精彩內(nèi)容