有1元,5元,10元,50元,100元,500元的硬币各c0,c1,c2,c3,c4, c5枚
现在要使用这些硬币支付n元,问最少需要多少枚硬币,假设至少存在一种方案。
应该尽可能使用500元的,然后再使用100元的.....使用贪心策略。
#include "stdafx.h"
#include<iostream>
#include<algorithm>
using namespace std;
const int V[6] = { 1, 5, 10, 50, 100, 500 };//硬币面额
int c[6];//每种面额硬币个数
int ans(int n)
{ int res = 0;for (int i = 5; i >= 0; i--){int t = min(n / V[i], c[i]);//如果当前不够,则为0,相当于不选择该种硬币.n -= t*V[i];res += t;}return res;
}
int main()
{int n, i;while (cin >> n){for (i = 0; i < 6; i++)cin >> c[i];cout << ans(n) << endl;}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。