仲有多一個pascal~~~~~救急~~~~10分~~唔該

2007-03-24 7:47 am
-write a program to change the input amount of money to 10-dollar, 5-dollar, 2 dollar, 1-dollar, 50-cent, 20-cent and 10-cent coins such that the number of coins is the least.
-sample output:

Enter amount of money: (78.3)
Number of $10 coins: 7
Number of $5 coins: 1
Number of $2 coins: 1
Number of $1 coins: 1
Number of $0.5 coins: 0
Number of $0.2 coins: 1
Number of $0.1 coins: 1

回答 (2)

2007-03-24 9:06 am
✔ 最佳答案
var amount: real;
tens, dollars, cents: integer;

begin
write("Enter amount of money:");
readln(amount);
tens := trunc(amount) div 10;
writeln("Number of $10 coins: ", tens);
amount := amount - 10 * tens;
dollars := trunc(amount);
cents := 10 * (amount - dollars);
writeln("Number of $5 coins: ", dollars div 5);
dollars := dollars - 5 * (dollars div 5);
writeln("Number of $2 coins: ", dollars div 2);
dollars := dollars - 2 * (dollars div 2);
writeln("Number of $1 coins: ", dollars);

writeln("Number of $0.5 coins: ", cents div 5);
cents := cents - 5 * (cents div 5);
writeln("Number of $0.2 coins: ", cents div 2);
cents := cents - 2 * (cents div 2);
writeln("Number of $0.1 coins: ", cents)

end.

2007-03-24 01:07:25 補充:
請勿移除問題, 我會記住你.
2007-03-25 6:56 pm
var am:real;
m:integer;
begin
write('Enter amount of money:');
readln(am);
m:=trunc((am+0.001)*10);
writeln(m);
writeln('Number of $10 coins:',m div 100);
m:=m mod 100;
writeln('Number of $5 coins:',m div 50);
m:=m mod 50;
writeln('Number of $5 coins:',m div 20);
m:=m mod 20;
writeln('Number of $1 coins:',m div 10);
m:=m mod 10;
writeln('Number of $0.5 coins:',m div 5);
m:=m mod 5;
writeln('Number of $0.2 coins:',m div 2);
m:=m mod 2;
writeln('Number of $0.1 coins:',m);
end.

解釋:
1. 輸入總數am(以實數輸入);
2. 把am化為以毫子為單位, 所以是am*10, 再化為整數, (am+0.001)是因為實數有時會有很微的精度誤差, 所以稍作補償,但不要影響有效的位數。把結果放到m中;
3. m div 100, 即是求m毫子可以有多少個10元;
4. m=m mod 100, 求餘數, 即是除了10元之外, 剩下多少尾數;
5. 之後原理喱3--4一樣, 分別求5元, 2元......

本題主要考學生使用Div及Mod的配合
參考: 20年程式設計經驗


收錄日期: 2021-04-12 21:11:38
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070323000051KK04825

檢視 Wayback Machine 備份