포스트

[Java] 백준 2480번 - 주사위 세개

Java 조건문과 Math.max를 활용해 주사위 세 개의 상금을 계산하는 풀이를 정리했습니다.

For the English version of this post, see here.
[Java] 백준 2480번 - 주사위 세개

BaekJoon 2480

풀이

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
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        
        if ((a == b) && (b == c)) {
            System.out.println(10000 + a*1000);
        } else if ((a == b) || (b == c) || (c == a)) {
            if (a == b) {
                System.out.println(1000 + a*100);
            } else if (b == c) {
                System.out.println(1000 + b*100);
            } else {
                System.out.println(1000 + c*100);
            }
        } else {
            int large = 0;
            if (a > b) {
                if (a > c) {
                    large = a;
                } else {
                    large = c;
                }
            } else if (b > c) {
                if (b > a) {
                    large = b;
                } else {
                    large = a;
                }
            } else {
                if (c > b) {
                    large = c;
                } else {
                    large = b;
                }
            }
            System.out.println(large*100);
            
        }
    }
}

Math.max

  • 두 값 중에서 더 큰 값을 반환하는 함수
  • 세개의 값 중 최댓값을 조건문으로 구하게 되면 풀이처럼 복잡해지기 때문에, Math.max를 사용해 쉽게 구할 수 있음
    1
    
    int large = Math.max(a, Math.max(b, c));