0
点赞
收藏
分享

微信扫一扫

问题 G: 找零钱

少_游 2022-01-27 阅读 62
算法

题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为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>
struct coin {
	int price;
	int num;
};
int main() {
	int n;
	while (scanf_s("%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) {
				n -= c[i].price;
				c[i].num++;
			}
			if (c[i].num != 0) {
				printf("%d*%d", c[i].price, c[i].num);
				if (n != 0) printf("+");
				continue;
			}
		}
		printf("\n");
	}
}

举报

相关推荐

0 条评论