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
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,552评论 5 6
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,949评论 0 23
  • 那一年我小学一年级 我交到了一个朋友 她是我的小前桌 她有一头长发 喜欢把头发扎起来 我一有功夫就揪她辫子 她让我...
    孤独的路灯阅读 405评论 1 3
  • 金秋九月,我们划着一条条航班弧线,从五湖四海集合到长春管院。 绿色的校园,清新的空气,蓝蓝的天,长春管院以朝气蓬勃...
    冷眼鲁娜阅读 656评论 0 0
  • 人还是不能活的太过于闭塞,不断与人结识相处的过程中你可以不断的自我提升、认识和改正。当然,一定的定力和判断力与改变...
    到底需要多少睡眠多少水阅读 253评论 0 0