新程序员,我需要来自 Harvard CS50 的 greedy.c 帮助

New programmer, I need help on greedy.c from Harvard CS50

我刚开始编程,所以我知道这可能是一个非常基本的错误,但我一直在尝试找出如何修复代码中的逻辑错误,以解决哈佛 CS50 的 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");
        }
    }
}

您不能真正将浮点数与整数 (0) 进行比较,因为某些深度汇编机制

你能做什么:

  1. 无条件printf("You need %i coins\n", coins)

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

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

谢谢大家的帮助。我最终听从了 Tardis 的建议,并改为使用整数进行计算。这是成功的!这是我最终得到的代码:

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

int main (void)
{
    printf("How much change is needed?\n");
    float tmp = get_float() * 100;//Gets amount owed from user in "x.xx" format
    int owed = round(tmp);
    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 >= 25)
    {
        owed = owed - 25;
        coins = coins + 1;
    }
    while (owed >= 10)
    {
        owed = owed - 10;
        coins = coins + 1;
    }
    while (owed >= 5)
    {
        owed = owed - 5;
        coins = coins + 1;
    }
    while (owed >= 1)
    {
        owed = owed - 1;
        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");
    }
}

喜欢终于把程序搞定的感觉。真希望我能自己想出这个:/不过,谢谢大家的建议。