leet-code/src/main/java/leetcode/editor/cn/NimGame.java
huangge1199@hotmail.com 455a153a96 292:Nim 游戏
2021-09-18 20:33:37 +08:00

65 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//你和你的朋友,两个人一起玩 Nim 游戏:
//
//
// 桌子上有一堆石头。
// 你们轮流进行自己的回合,你作为先手。
// 每一回合,轮到的人拿掉 1 - 3 块石头。
// 拿掉最后一块石头的人就是获胜者。
//
//
// 假设你们每一步都是最优解。请编写一个函数,来判断你是否可以在给定石头数量为 n 的情况下赢得游戏。如果可以赢,返回 true否则返回 false 。
//
//
//
//
// 示例 1
//
//
//输入n = 4
//输出false
//解释:如果堆中有 4 块石头,那么你永远不会赢得比赛;
// 因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。
//
//
// 示例 2
//
//
//输入n = 1
//输出true
//
//
// 示例 3
//
//
//输入n = 2
//输出true
//
//
//
//
// 提示:
//
//
// 1 <= n <= 2³¹ - 1
//
// Related Topics 脑筋急转弯 数学 博弈 👍 521 👎 0
package leetcode.editor.cn;
//292:Nim 游戏
class NimGame {
public static void main(String[] args) {
//测试代码
Solution solution = new NimGame().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean canWinNim(int n) {
return n % 4 > 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}