lleetcode 292. Nim Game

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.

本题中,由于你是第一个拿石子的人,所以,只要所有的石子数目为4的整数倍,那么就必然会输,因为你的对手可以保证在你拿走一次石子后,始终每次拿走石子的个数和你拿走石子的个数和为4。

class Solution {
public:
    bool canWinNim(int n) {
        if(n % 4 ==0)
            return false;
        else
            return true;
    }
};

  

时间: 2024-11-05 16:25:19

lleetcode 292. Nim Game的相关文章

292. Nim Game (取物游戏) by Python

292. Nim Game 题目: 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

292. Nim Game(C++)

292. Nim Game(C++) 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 firs

[LeetCode]-292. Nim Game(Easy)(C + 尼姆游戏)

292. Nim Game My Submissions Question Editorial Solution Total Accepted: 67907 Total Submissions: 128306 Difficulty: Easy 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

算法 - leetcode 292 Nim Game

算法 - leetcode 292 Nim Game  一丶题目 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏. 输入: 4 输出: false 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛: 因为无论你拿走 1 块.2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走. 二丶思路 1)

【算法功底】LeetCode 292 Nim Game

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 th

292. Nim Game

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 th

[leetcode] 292. Nim Game 解题报告

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 th

【一天一道LeetCode】#292. Nim Game.md

一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

LeetCode 292 Nim Game(Nim游戏)

翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确定对于给定的数字是否你能够赢得这场比赛. 比如,假设堆中有4个石头,那么你永远也无法赢得比赛:不管你移除了1.2或3个石头,最后一个石头都会被你的朋友所移除. 原文 You are playing the following Nim Game with your friend: There is a