Post

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

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

문제 링크


1. 문제 풀이


주사위의 세 눈이 모두 같은 경우 한 가지, 주사위의 두 눈이 서로 같은 경우 세 가지, 나머지 케이스로 분류하는 조건문을 활용하면 해결할 수 있다.


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

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


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

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

    int mx = 0;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        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;
}

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