描述
有1元、5元、10元、50元、100元、500元的钱币各C1,C5,C10,C50,C100,C500枚。现在要用这些钱币来支付A元,最少需要多少枚钱币?
输入
输入数据有多组,每组一行,每行7个数字,分别表示C1,C5,C10,C50,C100,C500和A。
输入以EOF结束。
0<=C1,C5,C10,C50,C100,C500<=10^9
0<= A <= 10^9
输出
每组输出一个整数,表示最少需要的钱币数目。
如果不能兑换,输出No Answer
样例输入
3 2 1 3 0 2 620
1 0 0 0 0 0 2
样例输出
6
No Answer
提示
500元1枚,50元2枚,10元1枚,5元2枚。
分析:
经典贪心问题。
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int money[6]={1,5,10,50,100,500};
int num[6],A;
while(cin>>num[0])
{
for (int i=1;i<6;i++)
cin>>num[i];
cin>>A;
int ans=0;
for (int i=5;i>=0;i–)
{
int T=min(A/money[i],num[i]);
A-=T*money[i];
ans+=T;
}
if (A==0) cout<<ans<<endl;
else
cout<<“No Answer”<<endl;
}
return 0;
}