[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
一、找零钱问题
例题1:
有 1 元,5元,10元,20元,100元,200元的钞票无穷多张。现在使用这些钞票支付X元,最少需要多少张钞票。
X = 628
最佳支付方法:
3张200块的,1张20块的,1张5块的,3张1块的
共需3+1+1+3 = 8张
直觉告诉我们:尽可能多的使用面值较大的钞票!
贪心法: 遵循某种规律,不断贪心的选取当前最优策略的算法设计方法。
分析:面额为1元、5元、10元、20元、100元、200元,任意面额是比自己小的面额的倍数关系。
所以当使用一张较大面额钞票时,若用较小面额钞票替换,一定需要更多的其他面额的钞票!
代码实现:
#include
#include
using namespace std;
int main(){
const int RMB[]= {200,100,20,10,5,1};
const int NUM = 6;//6种面值
int X = 628;
int count = 0;
for(int i= 0;i< NUM;i++){
int use = X / RMB[i];需要面额为RMB[i]的use张
count + = use;
X = X -RMB[i] * use;
printf("需要面额为%d 的%d张",RMB[i],use);
printf("剩余需要支付金额%d.\n",X);
}
printf("总共需要%d张\n",count);
return 0;
}
为何这么做一定是对的?
面额为 1元,5元,10元,20元,100元,200元,任意面额是比自己小的面额的倍数关系。
所以当使用一张较大面额钞票时,若使用较小面额钞票替换,一定需要更多的其他面额的钞票。
例如:
5=1+1+1+1+1
10=5+5
20=10+10
100=20+20+20+20+20
200=100+100
故:当前最优解即为全局最优解,贪心成立。
例题2:
有1元,5元,6元的纸币,现在用这些钞票支付K元,至少多少张纸币?
经我们分析,这种情况是不适合用贪心算法的,因为我们上面提供的贪心策略不是最优解。比如,要支付10元的话,按照上面的算法,至少需要1张6元的,4张1元的,而实际上最优的应该是2张5元的。
例题3:假设1元、2元、5元、10元、20元、50元、100元的纸币分别有a,b,c,d,e,f,g张。现在要用这些钱来支付m元,至少要用多少张纸币?如果能支付输出最少支付的张数,如果不能支付,输出-1。
#include
#include
using namespace std;
const int N=7;
int Count[N]={3,0,2,1,0,3,5}; //每种面值的数量
int Value[N]={1,2,5,10,20,50,100}; //面值
int solve(int money)
{
int num=0;
for(int i=N-1;i>=0;i--)
{
int c=min(money/Value[i],Count[i]);
money=money-c*Value[i];
num+=c;
}
if(money>0) num=-1;
return num;
}
int main()
{
int money;
cin>>money;
int res=solve(money);
if(res!=-1) cout<
else cout<
}
考虑一下,如果不同面值的钞票数量有限制,能不能直接用贪心算法。
返回目录:算法
上一篇:基本概念
Post Views:
278