选取硬币问题
有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;} }
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
轉載于:https://www.cnblogs.com/Thereisnospon/p/4768488.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: Codeforces 338 D. GC
- 下一篇: LoadRunner常用术语