题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为50 20 10 5 1 五种。请在知道要找多少钱n给小明的情况下,输出纸币数量最少的方案。 1<=n<=99;
输入
有多组数据 1<=n<=99;
输出
对于每种数量不为0的纸币,输出他们的面值*数量,再加起来输出
样例输入 Copy
25
32
样例输出 Copy
20 * 1+5 * 1
20 * 1+10 * 1+1 * 2
代码
#include<stdio.h>
struct coin {
int price;
int num;
};
int main() {
int n;
while (scanf("%d", &n) != EOF) {
coin c[5] = {{50, 0},
{20, 0},
{10, 0},
{5, 0},
{1, 0}};
for (int i = 0; i < 5; i++) {
while ((n - c[i].price) >= 0) {//注意一定使用while而不是if
n -= c[i].price;
c[i].num++;
}
if (c[i].num != 0) {//只有数量不为0的才需要输出
printf("%d*%d", c[i].price, c[i].num);
if (n != 0) printf("+");//中间连接
continue;
}
}
printf("\n");
}
return 0;
}