Post

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

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

문제 링크


1. 아이디어

각 사람의 상금을 같은 눈이 몇 개 나왔는지에 따라 세 경우로 계산해 최댓값을 구하는 문제다. 두 눈이 같은지만 검사하면 세 눈이 모두 같을 때도 조건을 만족하므로 세 눈이 같은 경우를 먼저 검사해야 한다. 같은 눈이 두 개일 때는 어느 두 주사위가 같은지 세 쌍을 모두 확인해 그 눈으로 상금을 계산한다.


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
28
29
30
31
32
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 max = 0;

        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());
            int c = Integer.parseInt(st.nextToken());

            if (a == b && b == c) {
                max = Math.max(max, 10000 + a * 1000);
            } else if (a == b) {
                max = Math.max(max, 1000 + a * 100);
            } else if (b == c) {
                max = Math.max(max, 1000 + b * 100);
            } else if (c == a) {
                max = Math.max(max, 1000 + c * 100);
            } else {
                max = Math.max(max, Math.max(a, Math.max(b, c)) * 100);
            }
        }

        System.out.println(max);
    }
}
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
31
#include <bits/stdc++.h>
using namespace std;

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

    int mx = 0;

    int n;
    cin >> n;

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

        if (a == b && b == c) {
            mx = max(mx, 10000 + a * 1000);
        } else if (a == b) {
            mx = max(mx, 1000 + a * 100);
        } else if (b == c) {
            mx = max(mx, 1000 + b * 100);
        } else if (c == a) {
            mx = max(mx, 1000 + c * 100);
        } else {
            mx = max(mx, max({a, b, c}) * 100);
        }
    }

    cout << mx;
}

std::max는 값 둘 외에 중괄호로 묶은 목록도 받으므로 std::max({a, b, c})로 세 값의 최댓값을 한 번에 구했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys

input = sys.stdin.readline

mx = 0

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

    if a == b == c:
        mx = max(mx, 10000 + a * 1000)
    elif a == b:
        mx = max(mx, 1000 + a * 100)
    elif b == c:
        mx = max(mx, 1000 + b * 100)
    elif c == a:
        mx = max(mx, 1000 + c * 100)
    else:
        mx = max(mx, max(a, b, c) * 100)

print(mx)

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