标签:硬币,int,sum,coins,number,C语言,mix,贪心 From: https://blog.csdn.net/weixin_52710247/article/details/139337006题目:有1元,5元,10元,100元,500元的硬币各从c1枚, c5枚,c10枚,c50枚,c100枚, c500枚,现在要用这些硬币支付A元,
最少需要多少枚硬币
输入:第一行有六个数字,分别代表从小到大6种面值的硬币的个数:第二行为A
案例:
输入:3 2 1 3 0 2
620
输出:
6#include<stdio.h> int main() { int number[6]; int A, sum = 0; int coins[6] = {1,5,10,50,100,500}; for (int j = 0; j < 6; j++) { scanf_s("%d",&number[j]); } scanf_s("%d",&A); for (int i = 5; A > 0; i--) { if (A > coins[i]) { int temp = number[i], mix, x; x = A / coins[i]; mix = x > temp ? temp : x; sum += mix; A = A - mix * coins[i]; } } printf("%d\n", sum); return 0; }