如何解决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 举报,一经查实,本站将立刻删除。