-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbachetsgame.java
43 lines (36 loc) · 959 Bytes
/
bachetsgame.java
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
34
35
36
37
38
39
40
41
42
package programming_challenges;
public class bachetsgame{
public static void main(String[] args) {
Kattio io = new Kattio(System.in);
while(io.hasMoreTokens()) {
int numStones = io.getInt();
int numMoves = io.getInt();
int[] moves = new int[numMoves];
for (int i = 0; i < numMoves; i++){
moves[i] = io.getInt();
}
boolean[] flag = new boolean[numStones+1];
flag[0] = true; //Stan can win with 1 stone
//when can Stan win
for (int i = 1; i <= numStones; i++){
for (int move: moves){
//not valid
if (i-move < 0) continue;
//can win with a single move
if (i-move == 0) {
flag[i] = true;
break;
}
//force Ollie to loose
if (!flag[i-move]){
flag[i] = true;
break;
}
}
}
if(flag[numStones]) System.out.println("Stan wins");
else System.out.println("Ollie wins");
}
io.close();
}
}