问题 G: 找零钱
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为50 20 10 5 1 五种。请在知道要找多少钱n给小明的情况下,输出纸币数量最少的方案。 1<=n<=99;
输入
有多组数据 1<=n<=99;
输出
对于每种数量不为0的纸币,输出他们的面值数量,再加起来输出
样例输入 Copy
25
32
样例输出 Copy
201+51
201+101+12
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<set>
using namespace std;
int main()
{
int n;
char num[5][3]={"50","20","10","5","1"};
while(scanf("%d",&n)!=EOF){
int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0;
while(n){
if(n>=50){
n-=50;
cnt1++;
}
else if(n<50&&n>=20){
n-=20;
cnt2++;
}
else if(n<20&&n>=10){
n-=10;
cnt3++;
}
else if(n<10&&n>=5){
n-=5;
cnt4++;
}
else{
n-=1;
cnt5++;
}
}
if(cnt1!=0){
printf("50*%d",cnt1);
if(cnt2!=0||cnt3!=0||cnt4!=0||cnt5!=0){
cout<<"+";
}
}
if(cnt2!=0){
printf("20*%d",cnt2);
if(cnt3!=0||cnt4!=0||cnt5!=0){
cout<<"+";
}
}
if(cnt3!=0){
printf("10*%d",cnt3);
if(cnt4!=0||cnt5!=0){
cout<<"+";
}
}
if(cnt4!=0){
printf("5*%d",cnt4);
if(cnt5!=0){
cout<<"+";
}
}
if(cnt5!=0){
printf("1*%d",cnt5);
}
cout<<endl;
}
return 0;
}