Post

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

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

문제 링크


1. 아이디어

라운드마다 두 주사위의 눈을 비교해 낮은 쪽이 상대의 눈만큼 점수를 잃고 같으면 아무도 잃지 않는 규칙을 100점에서 시작해 그대로 시뮬레이션한다. 잃는 점수는 이긴 쪽의 눈이므로 창영이가 이기면 상덕이가 창영이의 눈만큼, 상덕이가 이기면 창영이가 상덕이의 눈만큼 잃는다.


2. 코드

풀이 [Java][C++][Python]

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
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));

        int sumA = 100;
        int sumB = 100;

        int n = Integer.parseInt(br.readLine());
        while (n-- > 0) {
            StringTokenizer 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);
    }
}
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(0);
    cin.tie(0);

    int sumA = 100;
    int sumB = 100;

    int n;
    cin >> n;

    while (n--) {
        int a, b;
        cin >> a >> b;

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

    cout << sumA << '\n';
    cout << sumB << '\n';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sys

input = sys.stdin.readline

sum_a, sum_b = 100, 100

n = int(input())
for _ in range(n):
    a, b = map(int, input().split())

    if a > b:
        sum_b -= a
    elif a < b:
        sum_a -= b

print(sum_a)
print(sum_b)

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