✔ 最佳答案
//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;
}