개발자's Life

[Coding Test] 백준 - 주사위 세계 본문

코딩테스트

[Coding Test] 백준 - 주사위 세계

Rowen Jobs 2023. 3. 11. 01:24
728x90
반응형

문제

1에서부터 6까지의 눈을 가진 3개의 주사위를 던져서 다음과 같은 규칙에 따라 상금을 받는 게임이 있다. 

  1. 같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다. 
  2. 같은 눈이 2개만 나오는 경우에는 1,000원+(같은 눈)×100원의 상금을 받게 된다. 
  3. 모두 다른 눈이 나오는 경우에는 (그 중 가장 큰 눈)×100원의 상금을 받게 된다.  

예를 들어, 3개의 눈 3, 3, 6이 주어지면 상금은 1,000+3×100으로 계산되어 1,300원을 받게 된다. 또 3개의 눈이 2, 2, 2로 주어지면 10,000+2×1,000 으로 계산되어 12,000원을 받게 된다. 3개의 눈이 6, 2, 5로 주어지면 그중 가장 큰 값이 6이므로 6×100으로 계산되어 600원을 상금으로 받게 된다.

3개 주사위의 나온 눈이 주어질 때, 상금을 계산하는 프로그램을 작성 하시오.

입력

첫째 줄에 3개의 눈이 빈칸을 사이에 두고 각각 주어진다. 

출력

첫째 줄에 게임의 상금을 출력 한다.

 

짧게 짜고 싶었던 코드 였지만 좀 노다가 식으로 했다. 

import java.util.Scanner;

public class Main {
    
    public static int caculator(int dice_num){
        return 1000 + (dice_num * 100);
    }
	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		
		int first_dice = in.nextInt();
        int second_dice = in.nextInt();
        int third_dice = in.nextInt();
        int result_account = 0;
        
        if(first_dice == second_dice && second_dice == third_dice){
            result_account = 10000 + (first_dice * 1000);
        }else if(first_dice == second_dice){
            result_account = caculator(first_dice);
        }else if(second_dice == third_dice){
            result_account = caculator(second_dice);
        }else if(first_dice == third_dice){
            result_account = caculator(first_dice);
        }else{
            result_account = Math.max(Math.max(first_dice,second_dice),third_dice) * 100;
        }
        
        System.out.println(result_account);
	}
}

조건문에 대한 문제였고 처음 If 문은 세개의 숫자가 같을 때

else if 세개는 2개의 주사위가 같을 때 조건이고 

마지막 else 는 세개의 주사위가 다른 경우 계산식이다. 

728x90
Comments