十萬火急(freepascal)

2007-12-19 2:46 am
1.bingo遊戲, random 一組5位數,玩家再輸入5位數,若猜中數字,但不中位置以A表示,猜中數字也猜中位置以B表示。完全猜中輸出 bingo! 結束。 (5個數字不能重覆)
用freepascal作答!

回答 (1)

2007-12-19 10:13 am
✔ 最佳答案
個遊戲應該叫 mastermind, 唔係 bingo
我個版本係 Irie pascal, not free pascal, 你可以試試再修改下
同埋個遊戲好多假設,你可以再執執

program mastermind(output);
var
a : array[1..5] of integer;
b : array[1..5] of integer;
i,j : integer;
TypeA, TypeB : integer;
repeated : boolean;
guess : real;
start : integer;
begin
randomize;

for i := 1 to 5 do
repeat
repeated := false;
a[i] := trunc(random*10);
for j := 1 to i-1 do
if a[i] = a[j] then
repeated := true;
until NOT (repeated);


repeat
write("Please input a five digits number (99999 - quit): ");
readln(guess);
if guess > 99999 then
writeln("number out of range")
else if guess <> 99999 then
begin
start := 10000;
for i := 1 to 5 do
begin
b[i] := trunc(guess) div start;
guess := guess - b[i] * start;
start := start div 10;
end;
TypeA := 0;
TypeB := 0;
for i := 1 to 5 do
begin
if a[i] = b[i] then TypeA := TypeA + 1;
for j := 1 to 5 do
begin
if (a[i] = b[j]) and (i <> j) then TypeB := TypeB + 1;
end;
end;
if TypeA <> 5 then
for i := 1 to TypeA do write('A');
for i := 1 to TypeB do write('B');
for i := 1 to (5-TypeA-TypeB) do write('-');
writeln;
end;
until (TypeA = 5) or (guess = 99999);

for i := 1 to 5 do
begin
write(a[i]);
end;
writeln;
for i := 1 to 5 do
begin
write(b[i]);
end;

end.

2007-12-19 02:14:49 補充:
頭先無為意,我將 A 同 B 個意思倒轉左,請自行修改


收錄日期: 2021-04-13 14:44:52
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071218000051KK02648

檢視 Wayback Machine 備份