Post

[BaekJoon] 10103번 - 주사위 게임 [Java][C++]

[BaekJoon] 10103번 - 주사위 게임 [Java][C++]

문제 링크


1. 문제 풀이


창영이와 상덕이의 초기 점수를 각각 $100$ 점으로 세팅하고 창영이가 이기면 상덕이의 점수를 창영이의 주사위 눈만큼 빼주고 반대로 상덕이가 이기면 창영이의 점수를 빼주고, 비기면 그냥 넘어가면 된다.


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.*;
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 sumA = 100;
        int sumB = 100;

        int n = Integer.parseInt(br.readLine());
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());

            if (a > b) {
                sumB -= a;
            } else if (a < b) {
                sumA -= b;
            }
        }

        System.out.println(sumA);
        System.out.println(sumB);
    }
}


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

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

    int sumA = 100;
    int sumB = 100;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;

        if (a > b) {
            sumB -= a;
        } else if (a < b) {
            sumA -= b;
        }
    }

    cout << sumA << '\n';
    cout << sumB << '\n';
}

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