Post

[BaekJoon] 10886번 - 0 = not cute / 1 = cute [Java][C++]

[BaekJoon] 10886번 - 0 = not cute / 1 = cute [Java][C++]

문제 링크


1. 아이디어


0과 1의 등장 횟수를 비교만 해주면 되는 문제로 0의 개수에서 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.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());
        int cnt = 0;

        while (n-- > 0) {
            int x = Integer.parseInt(br.readLine());

            if (x == 0) {
                cnt++;
            } else {
                cnt--;
            }
        }

        if (cnt > 0) {
            System.out.println("Junhee is not cute!");
        } else {
            System.out.println("Junhee is cute!");
        }
    }
}


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
27
28
29
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;

    int cnt = 0;

    while (n--) {
        int x;
        cin >> x;

        if (x == 0) {
            cnt++;
        } else {
            cnt--;
        }
    }

    if (cnt > 0) {
        cout << "Junhee is not cute!";
    } else {
        cout << "Junhee is cute!";
    }
}

3. 디버깅


없음.


4. 참고


없음.


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