ddubi

백준 11047 : 동전 0 (Java) 본문

코테 문제풀이

백준 11047 : 동전 0 (Java)

ddubi__ 2022. 10. 25. 23:22

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

 

풀이

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

public class back_11047 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] data = br.readLine().split(" ");
		int N = Integer.valueOf(data[0]);
		int K = Integer.valueOf(data[1]);
		
		int[] list = new int[N];
		
		for(int i=N-1; i>=0; i--) {
			list[i] = Integer.valueOf(br.readLine());
		}
		
		int cnt = count(K,list);
		
		System.out.println(cnt);
	}

	private static int count(int money, int[] list) {
		int cnt = 0;
		for(int i=0; i<list.length; i++) {
			int target = list[i];
			// 뺄 수 있는 경우
			if(money>=target) {
				money = money - target;
				cnt ++;
				i--;
			}
		}
		return cnt;
	}

}
Comments