问题 G: 找零钱

问题 G: 找零钱

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值