[백준_Condition] 2480번 문제

Kwon·2023년 10월 23일

백준

목록 보기
4/22

사진 출처 : https://www.acmicpc.net/

백준 2480번 문제


풀이

이번 문제에는 조건문이 좀 있었기에 고민이 필요한 문제였다.

package b_Condition;

import java.util.Scanner;

public class Condition7 {

    public static int maxValue(int[] dice) {
        int maxValue = 0;
        for (int i = 0; i < dice.length - 1; i++) {
            maxValue = Math.max(dice[i], dice[i + 1]);
        }
        return maxValue;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] dice = new int[3];


        // 버블 정렬 이용
        int sameCount = 1;
        int sameValue = 0;
        int price = 0;

        for (int i = 0; i < dice.length; i++) dice[i] = sc.nextInt();

        for (int i = 0; i < dice.length - 1; i++) {
            for (int j = i; j < dice.length; j++) {
                if (i == j) continue;
                if (dice[i] > dice[j]) {
                    int temp = dice[i];
                    dice[i] = dice[j];
                    dice[j] = temp;
                } else if (dice[i] == dice[j]) {
                    sameCount++;
                    sameValue = dice[i];
                    System.out.println("SameCount : " + sameCount);
                }
            }
        }

        System.out.println(maxValue(dice));
        if (sameCount == 1) price = maxValue(dice) * 100;
        else if (sameCount == 2) price = 1000 + sameValue * 100;
        else price = 10000 + sameValue * 1000;

        System.out.println(price);
    }
}

좀 길게 했지만 다른 분들은 더 짧게 했을 것 같다...


같은 수 나올 상황 세개(1,2,3 쌍, sameCount)의 변수주사위 같은 면의 값(sameValue)얻을 상금(price)을 선언해주고 주사위 면(dice)을 삽입.

주사위 하나하나씩 기준을 잡아 같은 면이 몇 개 있는지 확인하기 위해 이중 for문 사용.

첫 if문은 기준 값과 확인하려는 값 서로 같은 값에 확인할 이유 없으니 continue로 무시.

두 번째 조건문은 가장 큰 값을 조사. 그게 아니라 서로 같은 값 이라면 같은 면이 얼마나 존재하는지 조사.


maxValue 함수를 이용해 가장 큰 눈 반환. 나머진 조건에 맞게 계산해 주었다.

결과

profile
📲 @bu_kwon_2 / 💻 dnu05043.log / ⌨ Back-end / 🦁 LikeLion

0개의 댓글