用C语言写出 人民名1分、2分、5分、1角、2角、5角、1元、2元、5元、10元、20元、50元、100元若干,要求输入任意金额钱数,编程实现以最少数量的人民币组成任意金额的币值;
时间: 2023-06-03 18:01:59 浏览: 96
这是一个编程问题,我可以回答。以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int money, n;
int values[] = {100, 50, 20, 10, 5, 2, 1}; // 所有面值,按从大到小顺序排列
int counts[7] = {0}; // 接收面值所需的最小数量
printf("请输入钱数(单位:元):");
scanf("%d", &money);
n = 0;
while (money > 0) {
if (money >= values[n]) {
money -= values[n];
counts[n]++;
} else {
n++;
}
}
printf("最少数量的人民币为:\n");
printf("%d 张100元\n", counts[0]);
printf("%d 张50元\n", counts[1]);
printf("%d 张20元\n", counts[2]);
printf("%d 张10元\n", counts[3]);
printf("%d 张5元\n", counts[4]);
printf("%d 张2元\n", counts[5]);
printf("%d 张1元\n", counts[6]);
return 0;
}
```
你可以将以上代码复制到C语言编译器中运行,输入任意金额,程序将输出组成该金额的最少人民币数量。