题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为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<stdio.h>
int main(){
int n;
while(scanf("%d",&n) != EOF) {
int rest[4] = {0},need[5] = {0};
need[0] = n / 50;
rest[0] = n % 50;
need[1] = rest[0] / 20;
rest[1] = rest[0] % 20;
need[2] = rest[1] / 10;
rest[2] = rest[1] % 10;
need[3] = rest[2] / 5;
rest[3] = rest[2] % 5;
need[4] = rest[3] /1;
if(need[0] != 0){ printf("%d*%d",50,need[0]); if(rest[0] != 0) printf("+"); else printf("\n");}
if(need[1] != 0){ printf("%d*%d",20,need[1]); if(rest[1] != 0) printf("+");else printf("\n");}
if(need[2] != 0){ printf("%d*%d",10,need[2]); if(rest[2] != 0) printf("+");else printf("\n");}
if(need[3] != 0){ printf("%d*%d",5,need[3]); if(rest[3] != 0) printf("+");else printf("\n");}
if(need[4] != 0){ printf("%d*%d",1,need[4]); printf("\n");}
}
return 0;
}