-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMisereNim.java
More file actions
34 lines (28 loc) ยท 878 Bytes
/
MisereNim.java
File metadata and controls
34 lines (28 loc) ยท 878 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
34
package hackerrank;
public class MisereNim {
static String misereNim(int[] s) {
// Nim๊ฒ์์ ์น์๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์
// ๋ ๋ช
์ ๊ฒ์ ์ฐธ๊ฐ์.
// ์ฐจ๋ก๊ฐ ์ฌ๋๋ง๋ค ๋ 1๊ฐ์ด์์ ๋๋ฌด๋ค์์ ์ ๊ฑฐํ๊ฑฐ๋ ๋๋ฌด๋ค์ ๋ํ ์ ์๋ค.
// ๋ง์ง๋ง ๋์ ์ ๊ฑฐํ๋ ์ฐธ๊ฐ์๊ฐ ์น๋ฆฌํ๋ค.
int xor = 0;
int sum = 0;
for (int i : s) {
xor ^= i;
sum += i;
}
if (sum == s.length && s.length % 2 == 0) {
return "First";
} else if (sum == s.length && s.length % 2 == 1) {
return "Second";
} else if (xor != 0) {
return "First";
} else {
return "Second";
}
}
public static void main(String[] args) {
System.out.println(misereNim(new int[]{1, 1}) + ", ans: First");
System.out.println(misereNim(new int[]{2, 1, 3}) + ", ans: Second");
}
}