gpt4 book ai didi

c - 新程序员,我需要哈佛 CS50 提供的有关greedy.c的帮助

转载 作者:行者123 更新时间:2023-11-30 20:33:35 25 4
gpt4 key购买 nike

我刚刚开始编程,所以我知道这可能是一个非常基本的错误,但我一直在尝试找出如何修复哈佛 CS50 类(class)的greedy.c 作业代码中的逻辑错误,但没有成功。我已经查找了该问题的解决方案,但它们似乎都以与我尝试的方式不同的方式解决它。我已经对其他示例进行了逆向工程,现在我理解了它们,但我真的很想知道如何使我自己的版本运行良好。

我试图通过一系列 while 循环来解决这个问题,每个循环从总欠款中减去一定的硬币值,然后将一个硬币添加到总硬币数中。对我来说,这在逻辑上似乎是有道理的,但是当我运行该程序时,它没有给我预期的输出。它只是不执行底部的 printf 语句。我希望你们中的一位能帮我解决这个问题!感谢您的帮助!

这是我的代码:

#include <stdio.h>
#include <cs50.h>

int main (void)
{
printf("How much change is needed?\n");
float owed = get_float();
int coins = 0;
/*While loops subtracting one coin from change owed, and adding one to coin count*/
while (owed >= 0.25)
{
owed = owed - 0.25;
coins = coins + 1;
}
while (owed >= 0.1)
{
owed = owed - 0.1;
coins = coins + 1;
}
while (owed >= 0.05)
{
owed = owed - 0.05;
coins = coins + 1;
}
while (owed >= 0.01)
{
owed = owed - 0.01;
coins = coins + 1;
}
/*While loops done, now print value of "coins" to screen*/
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
}

编辑:

所以我又尝试了一下,并完成了“if”语句。它为我返回错误,那么程序结束时“owed”的值怎么不等于0?

#include <stdio.h>
#include <cs50.h>

int main (void)
{
printf("How much change is needed?\n");
float owed = get_float(); //Gets amount owed from user in "x.xx" format
int coins = 0; //Sets initial value of the coins paid to 0
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed > 0.25)
{
owed = owed - 0.25;
coins = coins + 1;
}
while (owed > 0.1)
{
owed = owed - 0.1;
coins = coins + 1;
}
while (owed > 0.05)
{
owed = owed - 0.05;
coins = coins + 1;
}
while (owed > 0.01)
{
owed = owed - 0.01;
coins = coins + 1;
}
//While loops done, now print value of "coins" to screen
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
else
{
printf("Error\n");
}
}

编辑:

因此,一旦我的代码开始工作,我就开始摆弄它并进行过度设计。这是最终(目前)版本!

#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <time.h>



int main (void)
{
srand(time(0)); //generates random seed
float price = round(rand()%500); //generates random price between 0 and 500 cents
printf("You owe %f. How much are you paying?\n", price/100); //shows user their price between 0 and 5 dollars
printf("Dollars: ");
float paymnt = get_float()*100; //gets the amount user will pay in dollars then converts to cents

int owed = round (paymnt - price); //calculates the change owed by paymnt-price
int coins = 0; //Sets initial value of the coins paid to 0
int quarters= 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;

if (owed ==0 && price >0) //If someone pays in exact
{
printf("You paid the exact amount!\n");
}
else if (owed < 0) //If someone doesn't pay enough
{
printf("You didn't give us enough money!\n");
}
else //Else(We owe them change)
{
printf("Your change is %i cents\n", owed);
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed >= 25)
{
owed = owed - 25;
quarters = quarters + 1;
}
while (owed >= 10)
{
owed = owed - 10;
dimes = dimes + 1;
}
while (owed >= 5)
{
owed = owed - 5;
nickels = nickels + 1;
}
while (owed >= 1)
{
owed = owed - 1;
pennies = pennies + 1;
}
//While loops done, now print each coin and total coins needed to screen
if (owed == 0)
{
coins = quarters + dimes + nickels + pennies;
printf("You need %i coins (%i quarters, %i dimes, %i nickels, and %i pennies)\n", coins, quarters, dimes, nickels, pennies);
}
else
{
printf("Error\n");
}
}
}

最佳答案

您无法真正将 float 与整数(0)进行比较,因为某些深层汇编机制

你可以做什么:

  1. 只需执行 printf("You need %i coin\n", coin) 无条件

  2. if (owed <= 0.001 && owed >= -0.001)
    {
    printf("You need %i coins\n", coins);
    }

    这实际上是一种很常见的做法

关于c - 新程序员,我需要哈佛 CS50 提供的有关greedy.c的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44851727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com