硬币的计算误差最小的数 [英] Calculating smallest number of coins error

查看:241
本文介绍了硬币的计算误差最小的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始用C在线课程,和我通过PS1第二个问题的工作。这个问题需要我们从美元的用户要求变化的输入,并计算硬币数量最少的,你可以给他们这种变化,只要你是只允许25美分,10美分,5美分和1美分硬币。

I've recently started an online course in C, and am working through the second question in PS1. The question requires us to request an input of change from a user in dollars, and to calculate the smallest number of coins you can give them that change with, provided you are only allowed 25 cent, 10 cent, 5 cent and 1 cent coins.

例如50美分将是2月25日分的硬币和65美分将是2月25日美分硬币,1 10美分硬币和1个5分硬币。

For example 50 cents would be 2 25 cent coins, and 65 cents would be 2 25 cent coins, 1 10 cent coin and 1 5 cent coin.

下面是我的code:

#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'。我应该得到18金币(16 25分的硬币和2个10分的硬币),但程序会显示22它非常适用,但是我已经尝试了所有其他号码。

Now I am new to programming, so I am very aware that there are probably far more efficient ways of tackling this problem. However, this seems to work fairly well. However, strangely, I get an incorrect result when I try '4.2'. I should get 18 coins (16 25 cent coins and 2 10 cent coins), however the program displays 22. It works well for all other numbers I have tried however.

我想不出什么,我做错了。我觉得它或者是与在那里我被乘以100,或者,我在那里计算模和铸造变化为int改变美元美分,但遗憾的是我不能单独看着办吧。

I cannot figure out what I have done wrong. I feel like it either has something to do with where I change dollars to cents by multiplying by 100, OR, where I calculate the modulo and cast change to an int, but I unfortunately cannot figure it out alone.

我已经在很大程度上注明我的code所以它是比较容易理解。我希望有人能帮助我这个!

I have heavily annotated my code so it is somewhat easier to understand. I hope someone can help me with this!

推荐答案

正如其他人所指出的那样,浮点值不能总是被重新$ P $完美psented。为了解决这个错误,可以四舍五入到最接近的整数。这是一个很简单的功能入手:

As others have pointed out, floating point values cannot always be represented perfectly. To address the error, you can round to the nearest integer. Here is a very simple function to start with:

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

使用它在你的code是这样的:

Use it in your code like this:

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)

您将不得不与其他的情况进行实验,使其在所有条件下正常工作,但是这将让你开始。

You will have to experiment with corner cases to make it work well for all conditions, but this will get you started.

注意:这$ C四舍五入$ C例子是在简化,并提供了只是为了让你开始。这个问题确实是比较复杂的本 会话 处理四舍五入浮动到最接近的整数值。

Note: this code example for rounding is over simplified, and provided just to get you started. The problem is really more complicated as illustrated by this conversation dealing with rounding floats to nearest integer value.

这篇关于硬币的计算误差最小的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆