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

 

11047번: 동전 0

첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000) 둘째 줄부터 N개의 줄에 동전의 가치 Ai가 오름차순으로 주어진다. (1 ≤ Ai ≤ 1,000,000, A1 = 1, i ≥ 2인 경우에 Ai는 Ai-1의 배수)

www.acmicpc.net


그리디 알고리즘을 활용한 단순한 문제이다.
가장 큰수 부터 나눠지는대로 나눠서 몫을 cnt 값에 더해주기만 하면 끝이다.


 

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

public class Main {

	public static void main(String[] agrs) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		int N = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		int cnt = 0;

		int[] A = new int[N];

		for (int i = 0; i < N; i++)
			A[i] = Integer.parseInt(br.readLine());

		for (int i = N - 1; 0 <= i; i--) {
			int quotient = K / A[i]; // 몫

			if (quotient == 0)
				continue;
			else {
				K = K - A[i] * quotient;
				cnt += quotient;
			}
		}
		System.out.println(cnt);
	}
}

+ Recent posts