Cash: 課題
課題は、お釣りに何枚のコインが必要かを求めるプログラム。
https://cs50.harvard.edu/x/2021/psets/1/cash/

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);
}
未希 諒
香港在住の34歳サラリーマン。戦略家、写真家、旅人、サウナー。 2025年までに季節を選びながら世界中を飛び回る仕事をするために準備中。その道中で発見した気づきや気になる話題を中心に発信しています。2010年より旅をスタートし通算500箇所の宿に宿泊。年間の3分の1をホテルで暮らす。
Related posts
About media
Ramps Blog は、ノマドによるノマドのためのブログです。日本、香港、シンガポール、タイ、台湾のノマド生活を通じて見えた景色、特に食、カルチャー、テクノロジー、ビジネス、アート、デザインなどについて発信しています。
Recent Posts
Twitter feed is not available at the moment.