编写程序实现输出100元人民币的方案
1. 题目要求:
编写程序实现输出100元人民币兑换成1元、2元、5元人名币的所有兑换方案。
2. 题目分析:
- (1)题目要求100兑换成1元、2元、5元的人名币。在我的理解是每一次兑换人名币三种钱币都存在。即在for循环时,初始纸均设置为1,如果没有要求三种钱币都有的话,初始值可改为0;区别是多几种兑换方式。
- (2)钱币兑换
当兑换成5元时,100最多可以兑换20张。即:
for (five = 1; five <= 20; five++)
当兑换成2元时,100最多可以兑换50张。即:
for (tow = 1; tow <= 50; tow++)
当兑换成1元时,100最多可以兑换100张。即:
for (one = 1; one <= 100; one++)
3.代码展示:
(1)考虑所有钱币都存在:
#include <stdio.h>
int main()
{
int one, tow, five, count = 0;
for (five = 1; five <= 20; five++)
for (tow = 1; tow <= 50; tow++)
for (one = 1; one <= 100; one++)
{
if (5 * five + 2 * tow + one == 100)
count++;
}
printf("一共有%d种方案兑换", count);
return 0;
}
程序运行结果:
(2)不考虑所有钱币都存在:
#include <stdio.h>
int main()
{
int one, tow, five, count = 0;
for (five = 0; five <= 20; five++)
for (tow = 0; tow <= 50; tow++)
for (one = 0; one <= 100; one++)
{
if (5 * five + 2 * tow + one == 100)
count++;
}
printf("一共有%d种方案兑换", count);
return 0;
}
程序运行结果: