主题:[讨论]关于狐狸吃兔问题的讨论
我编了一道关于狐狸吃兔的程序 是用链表做的
program ex1;
type
point=^node;
node=record
data:integer;
next:point;
end;
var p,q,head:point;i,x,y:integer;a:array [1..10] of integer;
begin
for i:=1 to 10 do
a[i]:=1;
head:=nil;
for x:=1 to 10 do begin
if head=nil
then begin new(p);p^.data:=x;p^.next:=nil;q:=p;head:=p;end
else begin new(p);p^.data:=x;p^.next:=nil;q^.next:=p;q:=p;end;end;
p:=head;i:=0;
repeat
i:=i+1;
x:=-1;
repeat
[color=FF0000]if p<>nil then p:=p^.next;
if p=nil then p:=head;[/color]
x:=x+1;
until x=i;
y:=p^.data;
a[y]:=0;
until i=1000;
for i:=1 to 10 do
if a[i]=1 then write(i:4);
writeln;
end.
但是把程序中红字改成 [color=FF0000]if p<>nil then p:=p^.next else p:=head;[/color]
答案就不对了 请达人给予讲解 谢谢
program ex1;
type
point=^node;
node=record
data:integer;
next:point;
end;
var p,q,head:point;i,x,y:integer;a:array [1..10] of integer;
begin
for i:=1 to 10 do
a[i]:=1;
head:=nil;
for x:=1 to 10 do begin
if head=nil
then begin new(p);p^.data:=x;p^.next:=nil;q:=p;head:=p;end
else begin new(p);p^.data:=x;p^.next:=nil;q^.next:=p;q:=p;end;end;
p:=head;i:=0;
repeat
i:=i+1;
x:=-1;
repeat
[color=FF0000]if p<>nil then p:=p^.next;
if p=nil then p:=head;[/color]
x:=x+1;
until x=i;
y:=p^.data;
a[y]:=0;
until i=1000;
for i:=1 to 10 do
if a[i]=1 then write(i:4);
writeln;
end.
但是把程序中红字改成 [color=FF0000]if p<>nil then p:=p^.next else p:=head;[/color]
答案就不对了 请达人给予讲解 谢谢