forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokerNim.java
More file actions
24 lines (20 loc) ยท 777 Bytes
/
PokerNim.java
File metadata and controls
24 lines (20 loc) ยท 777 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
package hackerrank;
public class PokerNim {
static String pokerNim(int k, int[] c) {
// Nim๊ฒ์์ ์น์๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์
// ๋ ๋ช
์ ๊ฒ์ ์ฐธ๊ฐ์.
// ์ฐจ๋ก๊ฐ ์ฌ๋๋ง๋ค ๋ 1๊ฐ์ด์์ ๋๋ฌด๋ค์์ ์ ๊ฑฐํ๊ฑฐ๋ ๋๋ฌด๋ค์ ๋ํ ์ ์๋ค.
// ๋ง์ง๋ง ๋์ ์ ๊ฑฐํ๋ ์ฐธ๊ฐ์๊ฐ ์น๋ฆฌํ๋ค.
// XOR ์ฐ์ฐ์๋ ์ด์ง์๋ก ๋ ์์๊ฐ ๊ฐ์ผ๋ฉด 0, ๋ค๋ฅด๋ฉด 1๋ฅผ ์ถ๋ ฅํ๋ค.
int xor = c[0];
for (int i = 1; i < c.length; i++){
xor ^= c[i];
}
if(xor == 0) return "Second";
return "First";
}
public static void main(String[] args) {
System.out.println(pokerNim(5, new int[]{1,2})+", ans: First");
System.out.println(pokerNim(5, new int[]{2,1,3})+", ans: Second");
}
}