有1元,5元,10元,50元,100元,500元的硬币各C1,C5,C10,C50,C100,C500枚。现在要用这些硬币来支付A元,最少需要多少枚硬币?
#include <iostream>
using namespace std;
int V[6] = {1, 5, 10, 50, 100, 500};//硬币面值
int C[6] = {3, 2, 1, 3, 0, 2};//硬币对应的个数
int count = 0;
int A;
//求最小需要的硬币数
void Solve()
{
for(int i = 5; 0 <= i; --i)
{
int t = min(A / V[i], C[i]);
A -= V[i] * t;
count += t;
}
if(A == 0)
cout << count;
else
cout << "There is no way!";
}
int main()
{
cin >> A;
Solve();
return 0;
}