CS50贪婪的需求建议 [英] CS50 Greedy Need Advice

查看:84
本文介绍了CS50贪婪的需求建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理CS50问题集 Greedy。基本上问用户欠多少零钱,然后输出可以等于输入金额的最小硬币数量。效果很好,除了我输入4.2时输出22时应该输出18。

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


int main(void)
{
float n;
do
{
n = get_float(欠多少钱?\n);
}
while(n == EOF);

int美分=(int)(n * 100);
int minimumamountofcoins = 0;
if(cents / 25> = 1){
而(cents / 25> = 1)
{
cents-= 25;
Minimumamountofcoins ++;
}

}
如果(cents / 10> = 1){
而(cents / 10> = 1)
{
美分-= 10;
Minimumamountofcoins ++;
}
}
if(cents / 5> = 1){
而(cents / 5> = 1)
{
美分- = 5;
Minimumamountofcoins ++;
}
}
如果(cents / 1> = 1){
而(cents / 1> = 1)
{
美分- = 1;
Minimumamountofcoins ++;
}
}
printf(硬币的最小数量为%d\n,minimumaofofcoins);
}


解决方案

从float到int转换的问题。当您尝试将美元转换为美分时,可以使用以下代码行:

  int cents =(int)( n * 100); 

但是,此行代码为$ 4.20,返回的分值为419。这是一个舍入和浮点运算的问题,因为4.2 * 100返回419.99999999而不是420.0000000,并且整数转换截断而不是舍入。 $ 4.18以及其他可能的值也会出现此问题。



为防止这种情况,请在强制类型转换之前添加0.5,如下所示:

  int美分=(int)(n * 100 + 0.5); 

这将确保四舍五入的方向正确,因为您永远都不会

使用 math.h 库,您还可以使用 roundf()函数,该函数将在负数的情况下起作用,以防万一。

  int分=(int)(roundf(n * 100)); 


I am doing the cs50 problem set "Greedy". Basically asking the user for how much change is owed and then outputting the minimum amount of coins that can equal the inputed amount. It works perfectly, except when I input 4.2 it outputs 22 when it should output 18.

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


int main(void)
{
    float n;
    do
    {
        n = get_float("How much change is owed?\n");
    }
    while(n == EOF);

    int cents = (int)(n * 100);
    int minimumamountofcoins = 0;
    if (cents/25 >= 1){
        while (cents/25 >= 1)
        {
            cents -= 25;
            minimumamountofcoins++;
        }

    }
    if (cents/10 >= 1){
        while (cents/10 >= 1)
        {
            cents -= 10;
            minimumamountofcoins++;
        }
    }
    if(cents/5 >= 1){
        while (cents/5 >= 1)
        {
            cents -= 5;
            minimumamountofcoins++;
        }
    }
    if (cents/1 >= 1){
        while (cents/1 >= 1)
        {
            cents -= 1;
            minimumamountofcoins++;
        }
    }
    printf("The minimum amount of coins is %d\n", minimumamountofcoins);
}

解决方案

It looks like this is an issue with the conversion from float to int. When you try to convert from dollars to cents, you do so with this line of code:

int cents = (int)(n * 100);

However, this line of code, for $4.20, is returning a cent value of 419. This is a problem with rounding and floats, as 4.2 * 100 is returning 419.99999999 instead of 420.0000000, and integer casting truncates instead of rounding. This problem also occurs with $4.18, and probably other values as well.

To prevent this, add 0.5 before the cast, like so:

int cents = (int)(n * 100 + 0.5);

This will ensure that the rounding takes place in the proper direction, as you are never off by more that a trivial float error.

Using the math.h library, you could also use the roundf() function, which will work in the case of negative numbers, just in case.

int cents = (int)(roundf(n*100));

这篇关于CS50贪婪的需求建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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