[BaekJoon] 11868번 - 님 게임 2 [Java][C++]
[BaekJoon] 11868번 - 님 게임 2 [Java][C++]
1. 아이디어
전형적인 님 게임 문제로 각 돌 더미의 돌의 개수를 전부 XOR한 Nim-Sum이 0이 아니면 항상 0으로 만들 수 있는 선공이 이기며, 0이면 항상 0이 아닌 선택을 할 수 밖에 없어서 후공이 이긴다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int nimsum = 0;
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
while (n-- > 0) {
nimsum ^= Integer.parseInt(st.nextToken());
}
if (nimsum != 0) {
System.out.println("koosaga");
} else {
System.out.println("cubelover");
}
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int nimsum = 0;
int n;
cin >> n;
while (n--) {
int p;
cin >> p;
nimsum ^= p;
}
if (nimsum) {
cout << "koosaga";
} else {
cout << "cubelover";
}
}
3. 디버깅
없음.
4. 참고
없음.
This post is licensed under CC BY 4.0 by the author.