[Swift]LeetCode292. Nim游戏 | Nim Game

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bijwjyyy-kv.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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.github

Example:微信

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

你和你的朋友,两我的一块儿玩 Nim游戏:桌子上有一堆石头,每次大家轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你做为先手。函数

大家是聪明人,每一步都是最优解。 编写一个函数,来判断你是否能够在给定石头数量的状况下赢得游戏。spa

示例:code

输入: 
输出: false 
解释: 若是堆中有 4 块石头,那么你永远不会赢得比赛;
     由于不管你拿走 1 块、2 块 仍是 3 块石头,最后一块石头老是会被你的朋友拿走。4

1 class Solution {
2     func canWinNim(_ n: Int) -> Bool {
3         //若n=k*(m+1),则后取着胜
4         //反之,存在先取者获胜的取法。n%(m+1)==0 ,先取者必败
5         return n % 4 != 0
6     }
7 }
相关文章
相关标签/搜索