我正在制作一个简单的算法,该算法仅使用25c,10c,5c和1c硬币,返回键入$值时需要找回的最小硬币数目。
例如欠款额:0.31 硬币:3
请参见下面的代码,它会正确提示用户,但在输入时不会返回任何数字。
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
//Declaring the variables
float change_owed;
int quarter,dime,nickel,penny;
//Defining types of coins that can be given as change.
quarter = 25;
dime = 10;
nickel = 5;
penny = 1;
do
{
//Defining function and storing in variable change
change_owed = get_float("Change: ");
}
//Repeat prompt while input is negative
while (change_owed <= 0);
//rounding the numbers so we can divide
int amount = round(change_owed * 100);
///Deciding if to use a Quarter
//Defining Quarter counter
int count_quarter=0;
while (amount >= quarter)
{
//Counting number of quarters used
count_quarter++;
//Decreasing amount owed by a quarter
return amount - quarter;
}
//Deciding whether to use a dime
//Defining dime counter
int count_dime=0;
while (amount >= dime)
{
//Counting number of dimes used
count_dime++;
//Decreasing amount owed by a dime
return amount - dime;
}
int count_nickel=0;
while (amount >= nickel)
{
//Deciding whether to use a nickel
//counting number of nickels used
count_nickel++;
//Decreasing amount by a nickel
return amount - nickel;
}
//Deciding whether to use a penny
//Defining penny counter
int count_penny=0;
while (amount >= penny)
{
//counting number of pennies used
count_penny++;
//Decreasing amount by a penny
return amount - penny;
}
int total_coins = (count_quarter + count_dime + count_nickel + count_penny);
printf("%i",total_coins);
}