贪心算法找零钱

题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为50 20 10 5 1 五种。请在知道要找多少钱n给小明的情况下,输出纸币数量最少的方案。 1<=n<=99;
输入
有多组数据 1<=n<=99;
输出
对于每种数量不为0的纸币,输出他们的面值
数量,再加起来
样例输入
25
32
样例输出

20*1+5*1
20*1+10*1+1*2
#include <iostream>
#include<algorithm>
#include<map>
using namespace std;

int main(){
	//int m[5]={50,20,10,5,1};
	int n,leave;
	map<int,int> out;
	out[50]=0;
	out[20]=0;
	out[10]=0;
	out[5]=0;
	out[1]=0;
	while(scanf("%d",&n)){
	//scanf("%d",&n);
		leave=n;
		while(leave>0){
		if(leave>=50){
			out[50]+=1;
			leave-=50;
			continue;
		}

		if(leave>=20){
			out[20]+=1;
			leave-=20;
			continue;
		}
		 
        if(leave>=10){
			out[10]+=1;
			leave-=10;
			continue;
		}
		 
		if(leave>=5){
			out[5]+=1;
			leave-=5;
			continue;
		}
		 
		if(leave>=1){
			out[1]+=1;
			leave-=1;
			//printf("%d^^",leave);
			continue;
		}
	}

	  for(map<int,int>::iterator it = out.begin();it!=out.end();it++){
	  	 if(it->second>0){
	  	 	printf("%d*%d+",it->first,it->second);
		}
	  }
	  printf("\b ");
  }	
  return 0;
}

代码较粗糙,主要想锻炼自己用map实现,最后一个+号也不知道怎么消去,用来一个空格代替,机试估计不通过。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

miss writer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值