땀두 블로그

[백준] 11047번 - 동전 0 본문

알고리즘/백준

[백준] 11047번 - 동전 0

땀두 2022. 3. 22. 07:59

 

그리디 알고리즘 중 거스름돈 알고리즘을 이용한 간단한 문제이다. 가장 큰 금액부터 비교해가며 금액을 제외하고, 그 카운트를 출력해주어서 해결하였다.

 

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

public class p11047 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		int a = Integer.parseInt(st.nextToken());
		int b = Integer.parseInt(st.nextToken());
		int[] ary = new int[a];

		for (int i = 0; i < a; i++) {
			ary[i] = Integer.parseInt(br.readLine());
		}

		int cnt = 0;

		for (int i = a - 1; i >= 0; i--) {
			while (b >= ary[i]) {
				if (b >= ary[i]) {
					cnt++;
					b -= ary[i];
				}
			}
		}
		System.out.println(cnt);
	}
}
 

 

 

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 11021번 - A+B-7  (0) 2022.03.22
[백준] 15552번 - 빠른 A+B  (0) 2022.03.22
[백준] 2178번 - 미로 탐색  (0) 2022.03.22
[백준] 11659번 - 구간 합 구하기 4  (0) 2022.03.22
[백준] 11727번 - 2*n 타일링 2  (0) 2022.03.22
Comments