C中的贪心计算器
Greedy calculator in C
我需要创建一个只接受 .25
、.10
、.5
和 .1
的贪婪计算器。
我需要打印完成零钱所需的最少硬币数量。
这是一个循环,我看不出如何解决它。
我还在学习,放轻松:)
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
int coins = 0;
int change;
int i;
do {
change = get_float("How much change? ");
}
while (change <= 0); /* condition for acceptance*/
int cents = round(change * 100);
if (cents > 0) { /* run all whiles we get to 1c*/
while ((cents - 25) >= 25) { /* run 25c */
coins += 1;
}
while ((cents - 10) >= 10) { /* run 10c*/
coins += 1;
}
while ((cents - 5) >= 5) { /* run 5c*/
coins += 1;
}
while ((cents - 1) >= 1) { /* run 1c*/
coins += 1;
}
} else {
printf("%d", coins);
}
}
cents
变量永远不会改变。 (cents - 25)
确实 returns 当前美分的值减去 25,但您没有将它分配给 cents
变量,因此这 4 个 while 循环中的一个将始终为真。
还有一个问题:get_float
函数returnsfloat while change variable is int
,如果用户要输入一个小于1的值,比如.5,它将被转换为 0 并一次又一次地提示,直到用户输入大于 1 的值。另请注意,对于任何非整数输入,您可能会得到错误的答案。
您的代码中存在多个问题:
change
被定义为 int
,因此用户输入的金额甚至在计算开始之前就被截断,从而产生不正确的结果。
- 您没有在任何循环中更新
cents
,因此如果这些条件中的任何一个为真,您将得到一个无限循环。
注意条件不正确:如果美分大于或等于 50,则 (cents - 25) >= 25
为真。
测试 if (cents > 0)
不正确。如果 cents <= 0
,你只会打印硬币的数量。
- 最后一个循环没用了,剩下的美分数就是要数的便士数
这是修改后的版本:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
float change;
do {
change = get_float("How much change? ");
}
while (change <= 0); /* condition for acceptance*/
int cents = round(change * 100); /* avoid rounding issues */
int coins = 0;
while (cents >= 25) { /* count quarters */
cents -= 25;
coins += 1;
}
while (cents >= 10) { /* count dimes */
cents -= 10;
coins += 1;
}
while (cents >= 5) { /* count nickels */
cents -= 5;
coins += 1;
}
count += cents; /* count pennies */
printf("%d\n", coins);
return 0;
}
我需要创建一个只接受 .25
、.10
、.5
和 .1
的贪婪计算器。
我需要打印完成零钱所需的最少硬币数量。
这是一个循环,我看不出如何解决它。 我还在学习,放轻松:)
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
int coins = 0;
int change;
int i;
do {
change = get_float("How much change? ");
}
while (change <= 0); /* condition for acceptance*/
int cents = round(change * 100);
if (cents > 0) { /* run all whiles we get to 1c*/
while ((cents - 25) >= 25) { /* run 25c */
coins += 1;
}
while ((cents - 10) >= 10) { /* run 10c*/
coins += 1;
}
while ((cents - 5) >= 5) { /* run 5c*/
coins += 1;
}
while ((cents - 1) >= 1) { /* run 1c*/
coins += 1;
}
} else {
printf("%d", coins);
}
}
cents
变量永远不会改变。 (cents - 25)
确实 returns 当前美分的值减去 25,但您没有将它分配给 cents
变量,因此这 4 个 while 循环中的一个将始终为真。
还有一个问题:get_float
函数returnsfloat while change variable is int
,如果用户要输入一个小于1的值,比如.5,它将被转换为 0 并一次又一次地提示,直到用户输入大于 1 的值。另请注意,对于任何非整数输入,您可能会得到错误的答案。
您的代码中存在多个问题:
change
被定义为int
,因此用户输入的金额甚至在计算开始之前就被截断,从而产生不正确的结果。- 您没有在任何循环中更新
cents
,因此如果这些条件中的任何一个为真,您将得到一个无限循环。 注意条件不正确:如果美分大于或等于 50,则
(cents - 25) >= 25
为真。测试
if (cents > 0)
不正确。如果cents <= 0
,你只会打印硬币的数量。- 最后一个循环没用了,剩下的美分数就是要数的便士数
这是修改后的版本:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
float change;
do {
change = get_float("How much change? ");
}
while (change <= 0); /* condition for acceptance*/
int cents = round(change * 100); /* avoid rounding issues */
int coins = 0;
while (cents >= 25) { /* count quarters */
cents -= 25;
coins += 1;
}
while (cents >= 10) { /* count dimes */
cents -= 10;
coins += 1;
}
while (cents >= 5) { /* count nickels */
cents -= 5;
coins += 1;
}
count += cents; /* count pennies */
printf("%d\n", coins);
return 0;
}