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
다음 문제는 K 라는 가격을 동전의 가치로 만들어야 하는데 그 중에서 동전의 개수가 최소값이 되는 개수를 출력해야한다.
만약 K가 4200원일 때 (1000원 x 4개) + (100원 x 2개) 해서 6이 출력되어야 한다.
만약 K가 4790원일 때 (1000원 x 4개) + (500원 x 1개) + (100원 x 2개) + (50원 x 1개) + (10원 x 4개) 해서 12이 출력되어야 한다.
다음 아래 그림은 K가 4790원일 때 구하는 방법이다.
코드
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
int arr[] = new int[N];
int copy[] = new int[N];
int min=0;
int res=K;
int index=0;
int count=0;
for(int i=0;i<N;i++)
{
arr[i] = sc.nextInt();
}
while(res!=0)
{
for(int i=0;i<N;i++)
{
copy[i] = res/arr[i];
if(copy[i]>0)
{
min = Math.min(copy[i],copy[0]);
index=i;
}
}
res = res-arr[index]*min;
count+=min;
}
System.out.println(count);
}
}
'알고리즘 > 백준 풀이(그리디)' 카테고리의 다른 글
백준 14916번 - 거스름돈 (0) | 2024.01.23 |
---|---|
백준 1946번 - 신입 사원 (0) | 2024.01.19 |
백준 2217번 - 로프 (0) | 2024.01.18 |
백준 1026번 - 보물 (0) | 2024.01.17 |
백준 11399번 - ATM (0) | 2024.01.16 |