개발 공부 기록

[백준] 2480번 주사위 세개 - JAVA 본문

알고리즘

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

좨랭이 2022. 8. 18. 17:45
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		
		in.close();
		
        // 모두 같은 숫자인 경우
		if(a==b && a==c) {System.out.println(10000+a*1000);
        
        // 두 수만 같은 숫자인 경우
		} else if(a==b || a==c) {
			System.out.println(1000+a*100);
		} else if(b==c) {
			System.out.println(1000+b*100);
            
        // 모두 다른 수일 경우 제일 큰 수에 *100
		} else if(a!=b && b!=c && a!=c && a>b && a>c) {
			System.out.println(a*100);
		} else if(a!=b && b!=c && a!=c && a<b && b>c) {
			System.out.println(b*100);
		} else if(a!=b && b!=c && a!=c && a<c && b<c) {
			System.out.println(c*100);
		}
		
	}

}

모두 다른 수일 경우 a b c숫자 중 가장 큰 수를 일일이비교하여 출력할 수 있도록 하였다.

 

더 간단하게 하는 방법은 없을까 ... ? ? 

-> Math클래스의 max를 이용하는 방법이 있다

 

max 사용법

Math.max(값1, 값2)

값1과 값2 중 큰 값을 리턴함

 

 

수정

import java.util.*;

public class Math {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		
		in.close();
		
	// 모두 같은 숫자인 경우
		if(a==b && a==c) {System.out.println(10000+a*1000);
        
        // 두 수만 같은 숫자인 경우
		} else if(a==b || a==c) {
			System.out.println(1000+a*100);
		} else if(b==c) {
			System.out.println(1000+b*100);
            
        // 모두 다른 수일 경우 제일 큰 수에 *100
		} else if(a!=b && b!=c && a!=c) {
			System.out.println((Math.max(a, Math.max(b, c)))*100);
		} 
		
	}

}

세 개의 숫자를 비교할 땐 max를 중첩하여 사용해서 비교할 수 있다