본문 바로가기

algorithm

백준 2798 - 블랙잭

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cardsCount = scanner.nextInt(), targetNumber = scanner.nextInt();
        List<Integer> cards = new ArrayList<>(cardsCount);
        for(int i = 0; i < cardsCount; i++) cards.add(scanner.nextInt());
        System.out.println(findClosestNumber(targetNumber, cards));
    }

    // num에서 임의의 숫자 3개의 합 중 target에 가장 가까운 수를 찾는 함수
    public static int findClosestNumber(int target, List<Integer> num) {
        int theClosestN = 0;
        // i: 조합할 첫번째 수의 index
        // j: 조합할 두번째 수의 index
        // k: 조합할 세번째 수의 index
        for(int i = 0; i < num.size(); i++)
            // i >= j이면 동일한 수를 2번 꺼내게 되므로 j = i + 1 시작
            for(int j = i + 1; j < num.size(); j++) {
                // j >= k이면 동일한 수를 2번 꺼내게 되므로 k = j + 1 시작
                for(int k = j + 1; k < num.size(); k++) {
                    // n: 세 수를 조합한다
                    // dif: 조합한 수와 목표 숫자의 차
                    int n = num.get(i) + num.get(j) + num.get(k), dif = target - n;
                    if(dif >= 0 && dif <= target - theClosestN) {
                        theClosestN = n;
                        // dif가 0이면 이것보다 가까운 수를 찾을 수 없으므로 바로 반환한다.
                        if(dif == 0) return theClosestN;
                    }
                }
            }
        return theClosestN;
    }
}

문제링크: 2798 - 블랙잭

'algorithm' 카테고리의 다른 글

백준 1018 - 체스판 다시 칠하기  (0) 2021.10.26
백준 2231 - 분해합  (0) 2021.10.23
백준 1065 - 한수  (0) 2021.09.16
카펫  (0) 2021.04.11
소수 찾기  (0) 2021.04.10