Post

[BaekJoon] 11694번 - 님 게임 [Java][C++]

[BaekJoon] 11694번 - 님 게임 [Java][C++]

문제 링크


1. 아이디어


전형적인 Misere 님 게임 문제로 크기가 2 이상인 돌 더미가 하나라도 존재하는 경우 님 게임과 동일하게 승리할 수 있다. 반면 모든 돌 더미의 크기가 1인 경우는 기존 님 게임과 반대로 돌 더미의 수가 짝수 개여야 상대방이 마지막 돌을 가져갈 수 밖에 없다.


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
24
25
26
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 cnt = 0;

        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            int p = Integer.parseInt(st.nextToken());
            nimsum ^= p;
            if (p == 1) cnt++;
        }

        if ((cnt < n && nimsum != 0) || (cnt == n && cnt % 2 == 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
25
26
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int nimsum = 0;
    int cnt = 0;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        nimsum ^= p;
        if (p == 1) cnt++;
    }

    if ((cnt < n && nimsum != 0) || (cnt == n && cnt % 2 == 0)) {
        cout << "koosaga";
    } else {
        cout << "cubelover";
    }
}

3. 디버깅


없음.


4. 참고


없음.


This post is licensed under CC BY 4.0 by the author.