[英]c least amount of coins for amount entered
因此,我是新手,嘗試編寫一個程序以獲取金額為$ XX.XX的輸入,然后顯示最少數量的硬幣即可賺取該金額。 我制作了一個程序,但是在進行一些計算時(例如在13.49時)我經常花一分錢,但我不知道為什么。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int StayOn = 1;
while (StayOn == 1)
{
int x = 0;
float MoneyStart = 0, RemainingMoney = 0;
char cName[6][8] = {"Toonie", "Loonie", "Quarter", "Dime", "Nickel", "Penny"};
float cValue[6] = {2.00, 1.00, 0.25, 0.10, 0.05, 0.01};
int cCount[6] = {0, 0, 0, 0, 0, 0};
printf("Please Enter Amount: $");
scanf("%f", &MoneyStart);
getchar();
RemainingMoney = MoneyStart;
for (x = 0; x < 6; x++)
{
cCount[x] = (RemainingMoney / cValue[x]);
RemainingMoney = fmod(RemainingMoney, cValue[x]);
printf("%8s - %3i = $%.2f\n", cName[x], cCount[x], (cValue[x]*cCount[x]));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.