贪心算法编译没有错误,但在 运行 时永远不会超过第一个动作
Greedy algorithm compiles with no errors but never gets past first action when run
所以我正在学习 cs50,我正在做贪心算法 "cash" 问题,我编写的代码编译得很好,但是当我 运行 它时,它要求 "Change amount in USD:" 然后从不接受有效的响应,我一直在输入输入,什么也没有发生。
一位朋友说我缺少一个输入,这就是它不起作用的原因,但我正在尝试使用用户的输入,所以...非常感谢您的帮助,因为我想我'我真的很接近,我只是不知道如何解决它,help50 说技术上没有问题,所以我只是停滞不前。
谢谢!
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
// identifying variables
float amount;
int cents;
int count = 0;
// prompting user for input
do
{
amount = get_float("Change amount in USD: ");
}
// ensuring a positive number
while (amount < 0);
//changing float to an int
cents = round(amount*100);
// using highest denomination first (quarters)
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
// using next highest denomination second (dimes)
while (cents % 10 >= 0)
{
count++;
cents = cents % 10;
}
// using next highest denomination third (nickels)
while (cents % 5 >= 0)
{
count++;
cents = cents % 5;
}
// using last denomination amount (pennies)
while (cents % 1 >= 0)
{
count++;
cents = cents % 1;
}
// displays number of coins used
printf("%i\n", count);
}
你陷入了无限循环,因为你的逻辑是错误的。看一下 while 循环,例如:
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
模块操作会给你一个 0 到 24 之间的数字。无论原始值是多少,条件总是为真。
您想将您的条件更新为不同的条件。请注意,在您的 while 中,您应该以不同的方式更新美分,因为在第一次迭代后美分值不会改变:假设它是 10。它进入 while 循环,然后 cents=cents%25
将保持 10。仍然是无限循环
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
除法将永远是 >= 0
,因此你有一个无限循环,切换到
while (cents % 25 > 0)
问题在于这段代码(以及类似代码)
// using highest denomination first (quarters)
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
根据 any_integer_value % 25
始终是 >= 0
的原则,这将导致无限循环。但是,该问题实际上根本不需要 mod 操作。相反,检查减去硬币的价值是否为 >= 0
,如果是,则进行减法。
// using highest denomination first (quarters)
while (cents - 25 >= 0)
{
count++;
cents = cents - 25;
}
所以我正在学习 cs50,我正在做贪心算法 "cash" 问题,我编写的代码编译得很好,但是当我 运行 它时,它要求 "Change amount in USD:" 然后从不接受有效的响应,我一直在输入输入,什么也没有发生。
一位朋友说我缺少一个输入,这就是它不起作用的原因,但我正在尝试使用用户的输入,所以...非常感谢您的帮助,因为我想我'我真的很接近,我只是不知道如何解决它,help50 说技术上没有问题,所以我只是停滞不前。
谢谢!
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
// identifying variables
float amount;
int cents;
int count = 0;
// prompting user for input
do
{
amount = get_float("Change amount in USD: ");
}
// ensuring a positive number
while (amount < 0);
//changing float to an int
cents = round(amount*100);
// using highest denomination first (quarters)
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
// using next highest denomination second (dimes)
while (cents % 10 >= 0)
{
count++;
cents = cents % 10;
}
// using next highest denomination third (nickels)
while (cents % 5 >= 0)
{
count++;
cents = cents % 5;
}
// using last denomination amount (pennies)
while (cents % 1 >= 0)
{
count++;
cents = cents % 1;
}
// displays number of coins used
printf("%i\n", count);
}
你陷入了无限循环,因为你的逻辑是错误的。看一下 while 循环,例如:
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
模块操作会给你一个 0 到 24 之间的数字。无论原始值是多少,条件总是为真。
您想将您的条件更新为不同的条件。请注意,在您的 while 中,您应该以不同的方式更新美分,因为在第一次迭代后美分值不会改变:假设它是 10。它进入 while 循环,然后 cents=cents%25
将保持 10。仍然是无限循环
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
除法将永远是 >= 0
,因此你有一个无限循环,切换到
while (cents % 25 > 0)
问题在于这段代码(以及类似代码)
// using highest denomination first (quarters)
while (cents % 25 >= 0)
{
count++;
cents = cents % 25;
}
根据 any_integer_value % 25
始终是 >= 0
的原则,这将导致无限循环。但是,该问题实际上根本不需要 mod 操作。相反,检查减去硬币的价值是否为 >= 0
,如果是,则进行减法。
// using highest denomination first (quarters)
while (cents - 25 >= 0)
{
count++;
cents = cents - 25;
}