寫一個找錢的C程式

2009-08-16 1:03 am
鈔票有2000, 1000, 500, 200, 100
輸入金額:
2000:X張 1000:X張 500:X張 200:X張 100:X張
須用最少張數找錢..假如輸入金額都在百元以上

如:
輸入:12600
2000:6張 , 1000:0張, 500:1張, 200:0張, 100:1張,
我用C程式的陣列都寫不出來~請高手幫忙

回答 (2)

2009-08-16 6:28 am
✔ 最佳答案
//Headers and Macros
#include <stdio.h>
#include <stdlib.h>
//Main Function
void main(int argc, char* argv[])
{
//==START==//
//錢幣種類
const int nMoney[] = {2000, 1000, 500, 200, 100, 50, 10, 5, 1};
//我的金額,用來算錢的緩衝變數,書本價格
int My_Money, Money_Buffer, nPrize = 600;
//計數器
int i;
//錢幣種類的數目
int Money_Number = (int)sizeof(nMoney)/sizeof(nMoney[0]);
//用來統計的陣列指標
int *p;
printf("A Book prize is $ %d\nYou bought a Book.\nInput your Money: ", nPrize);
scanf("%d", &My_Money);
//如果大於 600 元的話
if (My_Money >= nPrize)
{
//建立一個統計的陣列
p = (int*)calloc(Money_Number, sizeof(nMoney[0]));
Money_Buffer = My_Money;
Money_Buffer -= nPrize;
for (i=0;i<Money_Number;i++)
{
p[i] = Money_Buffer / nMoney[i];
Money_Buffer -= (p[i] * nMoney[i]);
}
printf("$ %d Information: \n", My_Money);
for (i=0;i<Money_Number;i++)
{
if (p[i] != 0)
{
printf(" %d: x %d\n", nMoney[i], p[i]);
}
}
//釋放記憶體
free(p);
}
else
{
printf("You have to give me more money!!!\n");
}
//==END==//
system("PAUSE");
return;
}
2009-08-16 2:21 am
#include<stdio.h>
#include<stdlib.h>
int main()
{
int income,i,money[5]={2000,1000,500,200,100};
printf("輸入金額:");
scanf("%d",&income);
for(i=0;i<=4;i++){
printf("%d:%d張,",money[i],income/money[i]);
income%=money[i];
}
system("pause");
return 0;
}
參考: 自己


收錄日期: 2021-04-27 17:16:41
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090815000010KK06705

檢視 Wayback Machine 備份