首页 > 解决方案 > 对于 pset1 中的现金问题,如何使输出正常工作?

问题描述

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

int main(void)
{
    float a;
    int coins = 0;

    do 
    {
         a = get_float("Value of change in dollars: "); 
    }

    while (a < 0);
   {
        int change = round(a * 100);

        while (change / 25 >= 1)
        {
            coins++;
            change = change % 25;
        }

        while (change / 10 >= 1)
        { 
            coins++;
            change = change % 10;
        }

        while (change / 5 >= 1)
        {
            coins++;
            change = change % 5;
        }

        while (change / 2 >= 1)
        {
            coins++;
            change = change % 2;
        }

        while (change / 1 >= 1)
        {
            coins++;
            change = change % 1;
        }


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

}    

这些是我的输出:

:) cash.c exists
:) cash.c compiles
:) input of 0.41 yields output of 4
:) input of 0.01 yields output of 1
:) input of 0.15 yields output of 2
:( input of 1.6 yields output of 7
    expected "7\n", not "2\n"
:( input of 23 yields output of 92
    expected "92\n", not "1\n"
:( input of 4.2 yields output of 18
    expected "18\n", not "2\n"
:) rejects a negative input like -1
:) rejects a non-numeric input of "foo" 
:) rejects a non-numeric input of "" 

标签: ccs50

解决方案


推荐阅读