Post

[BaekJoon] 10102번 - 개표 [Java][C++]

[BaekJoon] 10102번 - 개표 [Java][C++]

문제 링크


1. 문제 풀이


$A$ 와 $B$ 의 등장 횟수를 비교만 해주면 된다.


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
27
28
import java.io.*;

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

        int a = 0;
        int b = 0;
        int v = Integer.parseInt(br.readLine());
        String str = br.readLine();

        for (int i = 0; i < v; i++) {
            if (str.charAt(i) == 'A') {
                a++;
            } else {
                b++;
            }
        }

        if (a > b) {
            System.out.println("A");
        } else if (a < b) {
            System.out.println("B");
        } else {
            System.out.println("Tie");
        }
    }
}


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

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

    int a = 0;
    int b = 0;

    int v;
    string s;
    cin >> v >> s;

    for (char c : s) {
        if (c == 'A') {
            a++;
        } else {
            b++;
        }
    }

    if (a > b) {
        cout << 'A';
    } else if (a < b) {
        cout << 'B';
    } else {
        cout << "Tie";
    }
}

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