pascal....點樣可以將以下既program唔會隨機咁搵重覆既number(急)

2006-10-24 7:11 am
program randomnumber;
var
number:array [1..100] of integer;
i,r,num,a,k:integer;
begin
write('times:');
readln(num);
for i:=1 to num do
begin
write('input ',i, ' :');
readln(number);
end;
randomize;
for k:=1 to 6 do
begin
r:= number[random(i)+1];
write(r, ' ');
end;
更新1:

應該係話..隨機咁重覆搵...而唔會搵重覆既number

更新2:

...我試左你既方法... 不過run到中間..就唔work... 但多謝你既幫忙

回答 (1)

2006-10-24 8:23 am
✔ 最佳答案
program randomnumber(Input, Output);

uses wincrt;

var number : array [1..100] of integer;
previous : array [1..5] of integer;
num, i, k, j, r, rval, repeatflag : integer;

begin
randomize;

write(’times:’);
readln(num);

for i := 1 to num do
begin
write(’input ’,i, ’ :’);
readln(number[i]);
end;

for k := 1 to 6 do
begin
repeatflag := 1;
while (repeatflag = 1) do
begin
rval := random(i) + 1;
j := 1;
repeatflag := 0;

while ((j <= (k - 1)) or (repeatflag = 1)) do
begin
if (rval = previous[j]) then
repeatflag := 1;

j := j + 1;
end;
end;

r := number[rval];
previous[k] := rval;
write(r, ’ ’);
end;
end.


收錄日期: 2021-04-23 15:45:06
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061023000051KK05280

檢視 Wayback Machine 備份