C _cashier中的贪婪算法

问题描述

我目前正在使用哈佛大学的CS50-CS简介,并且我以前没有编码经验。因此,我在分配作业上遇到了困难。

我的代码的主要目的是计算用于给定现金量的最小硬币数量(25、10、5和1)。此外,我必须将美元转换为美分。这是我的代码:

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

int main (void)
{

int change = 0;

 float dollars = get_float("Change: ");
 int cents = round( dollars * 100);  

while ( 0 > dollars ) 
; 
   
{
    if ( cents >= 25)
    {
        cents = cents - 25;
        change++;
    }
    else if ( cents >= 10)
    {
        cents = cents - 10;
        change ++;
    }
    else if ( cents >= 5)
    {
        cents = cents - 5;
        change++;
    }
    else if ( cents >= 1)
    {
        cents = cents - 1;
        change++;
    }
    printf("%i\n",change);
}
}

不幸的是,ı找不到错误所在。当我键入“ 5”时,它给我数字“ 1”。但是,在ı键入0.25时,它也给我结果“ 1”。有人可以帮忙吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...