题目:
有1,5,10,50,100,500面值的硬币,需要支付A元,最少需要支付多少枚硬币?假设至少存在一种支付方案。
输入:每种硬币的数量以及需要支付的钱
3 2 1 3 0 2
620
输出:6
思路:
每次从面值大的开始支付。
代码实现:
#include<iostream>
#include <algorithm>
using namespace std;
int velue[6]={1,5,10,50,100,500};//面值
int cnt[6];//对应面值的数量
int fun(int A,int index)
{
if(A<=0) return 0;
if(index==0) return A;
int x=A/velue[index];//包含该面值的数量
int t=min(x,cnt[index]);//实际用的该面值的数目
return t+fun(A-t*velue[index],index-1);//考虑下一轮更小的面值
}
int main()
{
for(int i=0;i<6;i++)
cin>>cnt[i];
int money;
cin>>money;
cout<<fun(money,5);//从最大面值开始
return 0;
}