计算最小数量的硬币错误

时间:2015-08-24 12:34:07

标签: c cs50

我最近在C开始了一个在线课程,正在处理PS1中的第二个问题。如果您只允许25美分,10美分,5美分和1美分硬币,那么这个问题要求我们要求用户以美元输入更改,并计算您可以给它们更改的最小硬币数量。

例如50美分将是2 25美分硬币,65美分将是2 25美分硬币,1美分硬币和1美分硬币。

这是我的代码:

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

int main(void)
{
    float change = 0;
    int coinCounter = 0;
    int remainder = 0;
    int remainder1 = 0;
    int remainder2 = 0;
    int remainder3 = 0;
.
    do
    {
        printf("Please enter the change in dollars: ");
        change = GetFloat();
    }
    while(change <= 0);

    //converts amount in dollars to cents
    change = change*100;

    //We want to use the largest number of big cois as possible to make up the change.
    // This calculates the remainder (if any) after dividing the change by 25. % = modulo, only works with integer operands.
    remainder = (int)change % 25;

    //(change - remainder) gets us the largest number divisible by 25. This line then calculates 
    // the maximum number of 25cent coins we can use, and sets this number equal to the coinCounter.
    coinCounter = ((int)change - remainder)/25;

    //Finds the remainder (if any) when dividing the last remainder by 10.
    remainder1 = remainder % 10;

    //(remainder - remainder1) gets us the largest number divisible by 10. Dividing this by 10, we
    // determine the max amount of 10 cent coins we can use to make up the required change. We then add 
    // this number of coins to the total coinCounter.
    coinCounter = coinCounter + ((remainder - remainder1)/10);

    //Again, take the remainder (if any) from the last calculation, and find the remainder when dividing by 5.
    remainder2 = remainder1 % 5;

    // (remainder1 - remainder2)/5 tells us the number of 5 cent coins we need to make up the required change. 
    // We add the number of coins to the coin counter.
    coinCounter = coinCounter + ((remainder1 - remainder2)/5);

    //Finds the remainder when dividing last remainder by 1. There will actually be no remainder, so remainder 3 will
    // equal zero. 
    remainder3 = remainder2 % 1;

    //Here, (remainder2 - remainder1)/1 Finds the number of 1 cent coins required to make up the left change.
    // remainder3 will always be zero. Hence (remainder2)/1 will always be equal to remainder 2. We add this number
    // of 1 cent coins to the coinCounter.
    coinCounter = coinCounter + ((remainder2 - remainder3)/1);

    //We print out coinCounter, which is the smallest number of coins required to make up the change.
    printf("%i\n",coinCounter);
}

现在我是编程新手,所以我非常清楚可能有更有效的方法来解决这个问题。但是,这似乎运作得相当好。然而,奇怪的是,当我尝试4.2&#39;时,我的结果不正确。我应该得到18个硬币(16个25美分硬币和2个10美分硬币),但程序显示22.它适用于我尝试过的所有其他数字。

我无法弄清楚我做错了什么。我觉得它要么与我通过乘以100来改变美元到美分的地方有关,或者,我计算模数并将更改转换为int,但遗憾的是我不能单独解决这个问题。

我对代码进行了大量注释,因此更容易理解。我希望有人可以帮助我!

3 个答案:

答案 0 :(得分:0)

我会做一些更简单的事情,比如:

 int main(void)
{
    float change = 0;
    int coinCounter = 0;
    int remainder = 0;
    int remainder1 = 0;
    int remainder2 = 0;

do
{
    printf("Please enter the change in dollars: ");
    change = GetFloat();
}
while(change <= 0);

change = change*100;
coinCounter = (int)change/25; //number of 25cents
remainder = change % 25; //rest

if(remainder > 0){
     coinCounter += (int)remainder/10; //number of 10cts
     remainder1 = remainder%10;
}

if(remainder1 > 0){
     coinCounter += (int)remainder1/5; //number of 5cts
     remainder2 = remainder1%5
}

coinCounter += remainder2; //number of 1cts

printf("%i\n",coinCounter);   

}

用4.2解决你的问题吗?我现在无法测试。

答案 1 :(得分:0)

问题是你已经将变量变量作为浮点数,而所有其他变量都是整数。

将float转换为int会引入小的不可预测的错误,并且在某些情况下可能会失败。

如果您对此感兴趣,那么有一篇非常好的文章here.

我建议您将输入视为美分,或者如果不可能,则取另一个整数变量并在程序开头转换为美分,并在计算中使用它。

答案 2 :(得分:0)

正如其他人所指出的,浮点值不能总是完美地表示。要解决该错误,您可以舍入到最接近的整数。这是一个非常简单的函数:

int round(float number)
{
    return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
}

在你的代码中使用它:

int main(void)
{
    float change = 0;
    int changed = 0;
    int coinCounter = 0;
    int remainder = 0;
    int remainder1 = 0;
    int remainder2 = 0;
    int remainder3 = 0;

do
{
    printf("Please enter the change in dollars: ");
    change = GetFloat();
}
while(change <= 0);

//converts amount in dollars to cents
changed = round(change*100);//changes 419... to 420 etc.
...(more code)

您必须尝试使用​​角落案例,以使其适用于所有条件,但这将帮助您入门。

注意 :这个用于舍入的代码示例已经过简化,只是为了让您入门。问题实际上更复杂,如 conversation 所示,处理将浮点数舍入为最接近的整数值。