题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为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实现,最后一个+号也不知道怎么消去,用来一个空格代替,机试估计不通过。