为什么我的C程序(现金,CS50)有53美分的盲点?

时间:2018-05-10 18:09:26

标签: c cs50

所以我为CS50构建了这个(迄今为止很棒的课程),但我不确定为什么我遇到的唯一错误是1-100美分是53,它只给出了4个硬币而不是正确的5个。我是C的新手,并且会喜欢第二双眼睛来看我的错误。

//Implement a program that calculates the minimum number of coins required to give a user change.

#include <stdio.h>
#include <cs50.h>
#include <math.h>


int main(void)
{

//get change owed, reject if negative
    float cash;
    printf("Change owed: ");

    do {
        cash= get_float();
    }
    while(cash<0);

//avoid imprecision by converting to cents
int cents = cash*100;

int count = 0;

//while(cash>1){
//    cash-1;
//    count++;
//}

while (cents>=25){
    count++;
    cents-=25;

}
while (cents>=10){
    count++;
    cents-=10;

}
while (cents>=5){
    count++;
    cents-=5;

}
while (cents>=1){
    count++;
    cents-=1;

}
printf("%i\n", count);

}

0 个答案:

没有答案