You and your friends, two people play Nim game together: There is a pile of rocks on the table, every time you take turns to remove 1-3 rocks. The one who removes the last stone is the winner. You act as the first mover.
You are smart people, every step is the best solution. Write a function to determine whether you can win the game with a given number of stones.
Example:
Input:
Output: false
Explanation: If there are 4 stones in the pile, then you will never win the game;
Because whether you take 1, 2, or 3 stones, the last stone will always be taken by your friend.
4
/div>
If the number of stones in the pilenn cannot be 44 divisible, then you Always can win the Nim game. p>
Inference
Let us consider some small examples. Obviously, if there is only one, two, or three stones in the pile of stones, then in your turn, you can take all the stones away and win the game. And if, as the title describes, there are exactly four stones in the pile, you will fail. Because in this case, no matter how many stones you take, you will always leave a few pieces for your opponent so that he can beat you in the game. Therefore, in order to win, you must avoid the situation where the number of stones in the pile of stones is 4 in your round.
Similarly, if there are five, six, or seven stones, you can control the number of stones you take, and always leave exactly four stones for your opponent to make him lose Drop this game. But if there are eight stones in the pile of stones, you will inevitably lose, because whether you pick one, two or three from the pile of stones, your opponent can choose three, two or one. To ensure that when it is your turn again, you will face four stones.
Obviously, it keeps repeating in the same pattern n=4,8,12,16,\dotsn=< span class="mspace">4,< span class="mord">8,12,16,…, you can basically tell it is 4A multiple of 4. span>
class Solution {
public< /span> boolean canWinNim(int n) {
return (n% 4 != 0);
}
}
You and your friend, two people play together Nim game: there is a game on the table Pile of stones, take one to three stones every time you take turns. The one who removes the last stone is the winner. You act as the first mover.
You are smart people, every step is the best solution. Write a function to determine whether you can win the game with a given number of stones.
Example:
Input:
Output: false
Explanation: If there are 4 stones in the pile, then you will never win the game;
Because whether you take 1, 2, or 3 stones, the last stone will always be taken by your friend.
4
You and your friends, two people play Nim game together: There is a pile of rocks on the table, every time you take turns to remove 1- 3 stones. The one who removes the last stone is the winner. You act as the first mover.
You are smart people, every step is the best solution. Write a function to determine whether you can win the game with a given number of stones.
Example:
Input:
Output: false
Explanation: If there are 4 stones in the pile, then you will never win the game;
Because whether you take 1, 2, or 3 stones, the last stone will always be taken by your friend.
4
Official solution:
If the number of stones in the pilenn cannot be 4 4 Divide, then you always can win the Nim game. p>
Inference
Let us consider some small examples. Obviously, if there is only one, two, or three stones in the pile of stones, then in your turn, you can take all the stones away and win the game. And if, as the title describes, there are exactly four stones in the pile, you will fail. Because in this case, no matter how many stones you take, you will always leave a few pieces for your opponent so that he can beat you in the game. Therefore, in order to win, you must avoid the situation where the number of stones in the pile of stones is 4 in your round.
Similarly, if there are five, six, or seven stones, you can control the number of stones you take, and always leave exactly four stones for your opponent to make him lose Drop this game. But if there are eight stones in the pile of stones, you will inevitably lose, because whether you pick one, two or three from the pile of stones, your opponent can choose three, two or one. To ensure that when it is your turn again, you will face four stones.
Obviously, it keeps repeating in the same pattern n=4,8,12,16,\dotsn=< span class="mspace">4,< span class="mord">8,12,16,…, you can basically tell it is 4A multiple of 4. span>
class Solution {
public< /span> boolean canWinNim(int n) {
return (n% 4 != 0);
}
}
class Solution {
public boolean canWinNim(int n) {
return (n% 4 != 0);
}
}