File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package hackerrank ;
2+
3+ public class MisereNim {
4+
5+ static String misereNim (int [] s ) {
6+ // Nim๊ฒ์์ ์น์๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์
7+ // ๋ ๋ช
์ ๊ฒ์ ์ฐธ๊ฐ์.
8+ // ์ฐจ๋ก๊ฐ ์ฌ๋๋ง๋ค ๋ 1๊ฐ์ด์์ ๋๋ฌด๋ค์์ ์ ๊ฑฐํ๊ฑฐ๋ ๋๋ฌด๋ค์ ๋ํ ์ ์๋ค.
9+ // ๋ง์ง๋ง ๋์ ์ ๊ฑฐํ๋ ์ฐธ๊ฐ์๊ฐ ์น๋ฆฌํ๋ค.
10+
11+ int xor = 0 ;
12+ int sum = 0 ;
13+
14+ for (int i : s ) {
15+ xor ^= i ;
16+ sum += i ;
17+ }
18+
19+ if (sum == s .length && s .length % 2 == 0 ) {
20+ return "First" ;
21+ } else if (sum == s .length && s .length % 2 == 1 ) {
22+ return "Second" ;
23+ } else if (xor != 0 ) {
24+ return "First" ;
25+ } else {
26+ return "Second" ;
27+ }
28+ }
29+
30+ public static void main (String [] args ) {
31+ System .out .println (misereNim (new int []{1 , 1 }) + ", ans: First" );
32+ System .out .println (misereNim (new int []{2 , 1 , 3 }) + ", ans: Second" );
33+ }
34+ }
You canโt perform that action at this time.
0 commit comments