forked from yangyiRunning/DataStructureAlgorithmsJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanWinNim.java
More file actions
33 lines (30 loc) · 951 Bytes
/
CanWinNim.java
File metadata and controls
33 lines (30 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ds;
/**
* Nim游戏
* <p>
* 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。
* <p>
* 示例:
* <p>
* 输入: 4
* 输出: false
* 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛;
* 因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。
*
* @author yangyi 2019年02月10日22:03:50
*/
public class CanWinNim {
/**
* 只要不是4的整数倍,你就稳赢
*/
public boolean canWinNim(int n) {
return n % 4 != 0;
}
public static void main(String[] args) {
CanWinNim canWinNim = new CanWinNim();
System.out.println(canWinNim.canWinNim(4));
System.out.println(canWinNim.canWinNim(7));
System.out.println(canWinNim.canWinNim(11));
System.out.println(canWinNim.canWinNim(16));
}
}