Cash: 課題

課題は、お釣りに何枚のコインが必要かを求めるプログラム。

https://cs50.harvard.edu/x/2021/psets/1/cash/

screenshot of Mario jumping over adjacent pyramids

 

Cash: 回答例

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

int main(void)
{
    float dollars;
    int cents;

    do
    {
        dollars = get_float("Change owed: ");
        cents = round(dollars * 100);
    }
    while (dollars < 0);

    int i = 0;

    // how many 25 cents coin used?
    while (cents >= 25)
    {
        cents = cents - 25;
        i++;
    }

    // how many 10 cents coin used?
    while (cents >= 10)
    {
        cents = cents - 10;
        i++;
    }

    // how many 5 cents coin used?
    while (cents >= 5)
    {
        cents = cents - 5;
        i++;
    }

    // how many 1 cent coin used?
    while (cents >= 1)
    {
        cents = cents - 1;
        i++;
    }

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

}