[백준] - 단계별로 풀어보기(조건문) 2480

김민경·2022년 4월 2일
0

백준

목록 보기
7/39

백준 2480번

주사위 세개

문제 출처 https://www.acmicpc.net/problem/2480


내가 작성한 코드

import java.util.Scanner;

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

접근 방법

  1. 눈 3개가 모두 값을 경우 (and 연산자 사용)
    a==b && b==c && a==c
  2. 눈 2개만 같은 경우 (or 연산자 사용)
    (a==b || b==c) 여기서 c==a 인 경우가 있을 수 있으므로 밑에 한번 더 조건을 걸어줘야 한다.
  3. 세개 눈이 모두 다른 경우 (else로 처리)
    이 중에서도 제일 큰 값을 구해야 하기 때문에 else안에서 조건을 걸어준다. 사실 Math.max()를 사용해주면 코드가 간단해진다.
Math.max(a, Math.max(b, c))*100

0개의 댓글

관련 채용 정보