6월 3주차 - 탐욕 [백준, 자바] 11047번: 동전 0

jinvicky·2024년 6월 9일
0

ALG

목록 보기
56/62
post-thumbnail

https://www.acmicpc.net/problem/11047

국룰 탐욕 알고리즘 문제.
ATM과 마찬가지인 방법으로 풀었다.
값들이 오름차순으로 정렬되어 있을 때, for문을 돌려서 가장 큰 동전부터 나눈 몫을 count에 더하고, 나머지값으로 기존 K를 갱신했다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line.split(" ")[0]);
        int K = Integer.parseInt(line.split(" ")[1]);
        int[] input = new int[N];

        for(int i = 0; i < N; i++) {
            input[i] = Integer.parseInt(br.readLine());
        }
        int count = 0;

        for(int i = N - 1 ; i >= 0; i--) {
            // 일단 큰 수부터 나누어서 몫을 구한다.
            count += K / input[i];
            // 나머지를 K로 만든다.
            K %= input[i];
        }
        System.out.println(count);
    }
}
profile
일단 쓰고 본다

0개의 댓글