当前位置:网站首页>[daily training -- Tencent selected 50] 292 Nim games

[daily training -- Tencent selected 50] 292 Nim games

2022-07-07 05:49:00 Puppet__

subject

You and your friends , Play with two people Nim game :

There is a pile of stones on the table .
You take turns in your rounds , You are the first to .
Every round , It's your turn to take it off 1 - 3 A stone .
The man who takes the last stone is the winner .
Let's assume that every step is the optimal solution . Please write a function , To determine if you can give a given number of stones n Win the game without . If you can win , return true; otherwise , return false .

Example 1:
Input :n = 4
Output :false
explain : Here are the possible results :

  1. remove 1 A stone . Your friend moved away 3 A stone , Including the last piece . Your friend won .
  2. remove 2 A stone . Move your friend 2 A stone , Including the last piece . Your friend won .
    3. You move 3 Stone . Your friend removed the last stone . Your friend won .
    Of all the results , Your friend is the winner .

Example 2:
Input :n = 1
Output :true

Example 3:
Input :n = 2
Output :true

Tips :
1 <= n <= 231 - 1

Code

class Solution {
    
    //  simplify nim game 
    public boolean canWinNim(int n) {
    
        //  As long as you always have four left for each other, you can guarantee your victory 
        return n % 4 != 0;
    }
}
原网站

版权声明
本文为[Puppet__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070002577010.html