c - C _cashier 中的贪心算法
问题描述
我目前正在参加哈佛的 CS50 - Introduction to 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”。任何人都可以帮忙吗?
解决方案
推荐阅读
- javascript - 在这种特殊情况下(通过共享调用),Javascript 会发生什么?
- ios - ASWebAuthenticationSession/SFAuthenticationSession中callbackURLScheme的作用是什么?
- java - java华氏到摄氏度,反之亦然转换器在jframe中
- web - 我将tailwindCSS 用于一个简单的html 站点,但在netlify 上部署不起作用,为什么?
- powershell - Powershell 更改文本
- sql - 在这样的表 R(ABCDE) 上,我需要一个 SQL 触发器,以防止元组的 A 和 B 列相同但尝试为 C 赋予不同的值
- css - 创建三个单独按钮的垂直堆栈
- ios - 包含静态预填充和动态用户数据的核心数据
- python - 无法在 Python 中导入名称模型
- c# - VS2017 GAC Microsoft Team Foundation DLLl 参考问题