PASCAL程序求修改

来源:百度知道 编辑:UC知道 时间:2024/06/19 04:01:54
是猴子选大王问题,用的是队列,不过是首尾相连的.
var q:array[1..1000000] of longint;
n,i,f,r,m,x:longint;

procedure enq(x:longint);
begin
q[r]:=x;
r:=r+1;
end;

procedure deq(x:longint);
begin
x:=q[f];
f:=f+1;
end;

function empty:boolean;
begin
empty:=(f=r);
end;

begin
readln(n,m);
f:=1; r:=1;
for i:=1 to n do enq(i);
while not empty do
begin
for i:=1 to m-1 do
begin
deq(x);
enq(x);
end;
deq(i);
writeln(i);
end;
end.
请哪位高手帮帮忙,谢谢!

var
a:array[0..1000]of boolean;
n,k,i,j,l:integer;
begin
read(n,k);
for i:=0 to n-1 do a[i]:=true;
i:=-1;l:=n;
while l>1 do begin
j:=0;
while j<k do begin
i:=(i+1)mod n;
if a[i] then inc(j);
end;
a[i]:=false; dec(l);
end;
for i:=0 to n do if a[i] then write(i+1);
end.

设置我为最佳答案

var
a:array[0..1000]of boolean;
n,k,i,j,l:integer;
begin
read(n,k);
for i:=0 to n-1 do a[i]:=true;
i:=-1;
while l<n do begin
while j<k do begin
i:=(i+1)mod n;
if a[i] then inc(j);
end;
a[i]:=false; inc(l);
j:=0;
if l=n then begin
writeln(i+1);
exit;
end;
end;
end.