c - cs50 pset1 现金-贪婪挑战
问题描述
这是cs50中的一个问题,它指出
实现一个程序,计算给用户找零所需的最小硬币数量。
问题的输出是要给用户的硬币数量。我制作了程序,它运行得几乎完美,但是对于 4.2 美元,输出是 22,但它应该是 18。输出的所有其他值都是正确的。谁能告诉这段代码有什么问题?
#include<stdio.h>
#include<cs50.h>
int main(void)
{
float change_owed, change_owed_in_hundreds;
int number_of_coins_to_be_given = 0;
do
{
printf("enter the positve ammount of change owed in dollars");
change_owed = get_float();
}while(c < 0);
change_owed_in_hundreds = change_owed * 100;
while(change_owed_in_hundreds >= 25){
change_owed_in_hundreds = change_owed_in_hundreds - 25;
number_of_coins_to_be_given++;
}
while(change_owed_in_hundreds >= 10) {
change_owed_in_hundreds = change_owed_in_hundreds - 10;
number_of_coins_to_be_given++;
}
while(change_owed_in_hundreds >= 5) {
change_owed_in_hundreds = change_owed_in_hundreds - 5;
number_of_coins_to_be_given++;
}
while(change_owed_in_hundreds >= 1) {
change_owed_in_hundreds = change_owed_in_hundreds - 1;
number_of_coins_to_be_given++;
}
printf("%d\n", number_of_coins_to_be_given);
}