好吧,假设出纳员欠客户一些零钱,而出纳员的抽屉中有25美分,10美分,5美分和1美分的便士。要解决的问题是确定要交付给客户的硬币和硬币数量
如果某些客户欠款41美分,则可以收取的最大第一笔费用为25美分 41-25 = 16 再咬25美分会太大 收银员会咬到10美分,使他或她有6美分的问题 那时收银员要求先咬5美分,然后再咬1美分
<TargetFrameworks>
#include <cs50.h>
#include <stdio.h>
int main(void)
{
float f = get_float("Enter Cash: ");
int q;
int d;
int n;
int p;
float quarter = 0.25;
float dimes = 0.10;
float nickels = 0.05;
float pennies = 0.01;
while ( f != 0)
{
if (f >= quarter){
f = f - quarter;
q = q + 1;
} else if (f >= dimes && f < quarter) {
f = f - dimes;
d = d + 1;
} else if (f >= nickels && f < dimes) {
f = f - nickels;
n = n + 1;
} else {
f = f - pennies;
p = p + 1;
}
printf ("Quarter: %d \n Dimes %d \n Nickels %d \n Pennies %d \n", q,
d, n, p);
}
}
答案 0 :(得分:2)
您需要初始化变量
int q = 0;
int d = 0;
int n = 0;
int p = 0;
否则它们具有不可预测的值