Greedy.c输出错误的数字,我不明白为什么? (CS50代码)

时间:2016-02-20 18:42:00

标签: c greedy cs50

我正在研究一个程序,以计算出用户给出的最小数量的硬币。

用于检查0.25,0.10,0.05和0.01的值。如果用户的值大于或等于其中一个(在一组连续的if语句中),则额外的硬币被添加到硬币计数中,并且该金额将从用户的原始数据中删除号。

然后,当用户的号码小于0.01时,该程序应该结束并打印硬币计数。

我已经多次阅读过这段代码并尝试修复但我无法解决问题,你可以帮我解决吗?

0.25显示1个硬币(正确) 但 0.26显示1个硬币(错误),由于某种原因错过了我后来的if语句。

除了硬币值之外,还会复制和粘贴控制0.25和0.01的2个代码块,那么接下来会发生什么?

谢谢, Raisu

#include <stdio.h>
#include <cs50.h>
int main (void)
{
    /**  
    * Declare all variables
    */
    float UserInput  =  0.00;
    float Coin1      =  0.25;
    float Coin2      =  0.10;
    float Coin3      =  0.05;
    float Coin4      =  0.01;
    int   CoinCount  =  0;
    /**
    * Ask user for change amount and check it is more than 0, then print it
    */ 
    do
    {
        printf("Please put the amount of change, in the format 0.00\r\n\r\n");
        UserInput = GetFloat();// Get a float, check format
    }
    while (UserInput <0 || UserInput == 0);

    printf ("You have entered %f\r\n\r\n",UserInput);
    /**
     * If userinput is over or equal to 0.25, take off 0.25 and add 1 to the coin count
     */ 
    if (UserInput>=0.25) 
    {
        do
        {
            UserInput -= Coin1;
            CoinCount +=1;
        }
        while (UserInput >= Coin1);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin1);
    }
    /**
    * If userinput is over or equal to 0.10, take off 0.10 and add 1 to the coin count
    */ 
    if (UserInput >=0.10)
    {
        do
        {
            UserInput -= Coin2;
            CoinCount +=1;
        }
        while (UserInput >= Coin2);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin2);
    } 
    /** 
     * If userinput is over or equal to 0.05, take off 0.05 and add 1 to the coin count
     */ 
    if (UserInput >=0.05) 
    {
        do
        {
        UserInput -= Coin3;
        CoinCount +=1;
        }
        while (UserInput >= Coin3);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin3);
    }
    /**
     * If userinput is over or equal to 0.01, take off 0.01 and add 1 to the coin count
     */
    if (UserInput >=0.01) 
    {
        do
        {
            UserInput -= Coin4;
            CoinCount +=1;
        }
        while (UserInput >= Coin4);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin4);

    } 
    printf("\r\nTotal Coins Needed: %i\r\n*********************\r\n\r\n\r\n",CoinCount);
}

2 个答案:

答案 0 :(得分:1)

我编写了这个小程序来说明浮点格式无法准确存储所有值的问题。

#include <stdio.h>

int main(void){
    float UserInput;
    int UserCents;
    printf("Please put the amount of change, in the format 0.00\n");
    if (scanf("%f", &UserInput) != 1)
        return 1;
    UserCents = (int)(UserInput * 100 + 0.5);
    printf("Dollars = %.15f\n", UserInput); 
    printf("Cents   = %d\n", UserCents); 
    return 0;
} 

计划会议:

Please put the amount of change, in the format 0.00
12.34
Dollars = 12.340000152587891
Cents   = 1234

所以你有两个途径,1)切换到使用int,或2)将浮点值与容差进行比较。永远不要将它们比作平等。

答案 1 :(得分:0)

我修改了你的代码。这工作正常。 (我尝试了几个输入)。

int main (void)
{
    /**  
    * Declare all variables
    */
    double UserInput  =  0.00;
    double Coin1      =  0.25;
    double Coin2      =  0.10;
    double Coin3      =  0.05;
    double Coin4      =  0.01;
    int   TotalCoinCount  =  0;
    int CoinCount = 0;
    /**
    * Ask user for change amount and check it is more than 0, then print it
    */ 
    do
    {
        printf("Please put the amount of change, in the format 0.00\r\n\r\n");
        //UserInput = GetFloat();// Get a float, check format
        cin >> UserInput;
    }
    while (UserInput <0 || UserInput == 0);

    printf ("You have entered %f\r\n\r\n",UserInput);
    UserInput = floorf(UserInput * 100) / 100;
    /**
     * If userinput is over or equal to 0.25, take off 0.25 and add 1 to the coin count
     */     
    if (UserInput>=0.25) 
    {
        do
        {
            UserInput -= Coin1;
            UserInput = floorf(UserInput * 100) / 100;
            CoinCount +=1;
        }
        while (UserInput >= Coin1);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin1);
    }
    TotalCoinCount += CoinCount;
    CoinCount = 0;
    /**
    * If userinput is over or equal to 0.10, take off 0.10 and add 1 to the coin count
    */ 
    if (UserInput >=0.10)
    {
        do
        {
            UserInput -= Coin2;
            UserInput = floorf(UserInput * 100) / 100;
            CoinCount +=1;
        }
        while (UserInput >= Coin2);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin2);
    } 
    TotalCoinCount += CoinCount;
    CoinCount = 0;
    /** 
     * If userinput is over or equal to 0.05, take off 0.05 and add 1 to the coin count
     */ 
    if (UserInput >=0.05) 
    {
        do
        {
        UserInput -= Coin3;
        UserInput = floorf(UserInput * 100) / 100;
        CoinCount +=1;
        }
        while (UserInput >= Coin3);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin3);
    }
    TotalCoinCount += CoinCount;
    CoinCount = 0;
    /**
     * If userinput is over or equal to 0.01, take off 0.01 and add 1 to the coin count
     */
    if (UserInput >=0.01) 
    {
        do
        {
            UserInput -= Coin4;
            UserInput = floorf(UserInput * 100) / 100;
            CoinCount +=1;
        }
        while (UserInput >= Coin4);
        printf ("\r\n%i of %f Pieces\r\n",CoinCount,Coin4);      
    } 
    TotalCoinCount += CoinCount;
    CoinCount = 0;
    printf("\r\nTotal Coins Needed: %i\r\n*********************\r\n\r\n\r\n",TotalCoinCount);
}

我已将float更改为double,并使用floorf()来避免任何双重数据精确损坏。