Fork me on GitHub

硬币问题 --贪心

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int arr [] = new int [6];
		int min = 0 ; 
		for(int i = 0 ; i < 6 ; i++) {
			arr[i] = sc.nextInt();
		}
		int money[] ={1,5,10,50,100,500};
		int sum = sc.nextInt();
		for(int i =5 ; i>=0 ; i--) {
			min += Math.min(sum/money[i], arr[i]);
			sum -= Math.min(sum/money[i], arr[i])*money[i];
		}
		System.out.println(min);
	}
}
posted @ 2019-07-07 22:09  cznczai  阅读(141)  评论(0编辑  收藏  举报