[BOJ] 그리디 알고리즘/11047번 - 동전 0

1083 단어 JavabojJava

문제

제출답안

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String inputStr = sc.nextLine();
		String[] splitStr = inputStr.split(" ");
		int numberOfCoin = Integer.parseInt(splitStr[0]);	// 동전의 종류
		int targetAmount = Integer.parseInt(splitStr[1]);	// 구하고자하는 동전 금액의 합
		
		int[] coin = new int[numberOfCoin];
		for(int i=0; i<coin.length; i++) {		// 입력한 동전의 종류만큼 각각의 동전 입력
			coin[i] = sc.nextInt();
		}
		
		int count = 0;
		for(int j=coin.length-1; j>=0; j--) {
			if(coin[j] <= targetAmount) {
				count += targetAmount / coin[j];	//	동전의 사용횟수를 추가
				targetAmount = targetAmount % coin[j];	// 사용한 동전개수만큼 합에서 차감
			}
		}
        
		System.out.println(count);
        sc.close();
	}
	
}

출처
백준 알고리즘

좋은 웹페이지 즐겨찾기