[BaekJoon] #10103 - 주사위 게임 [Java][C++]
[BaekJoon] #10103 - 주사위 게임 [Java][C++]
1. 아이디어
창영이와 상덕이의 초기 점수를 각각 100점으로 세팅하고 창영이가 이기면 상덕이의 점수를 창영이의 주사위 눈만큼 빼고, 반대로 상덕이가 이기면 창영이의 점수를 빼고, 비기면 그냥 넘어가면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(1)$ |
($N$ = 입력값 n)
3. 코드
풀이 [Java][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
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());
while (n-- > 0) {
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 sum_a = 100;
int sum_b = 100;
int n;
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
if (a > b) {
sum_b -= a;
} else if (a < b) {
sum_a -= b;
}
}
cout << sum_a << '\n';
cout << sum_b << '\n';
}
This post is licensed under CC BY 4.0 by the author.