D
Dale46
Unregistered / Unconfirmed
GUEST, unregistred user!
约瑟夫问题:
10个猴子,围成一圈,依次报数,从1到3, 1,2,3,1,2,3,1,2,3...
凡是数到3的就出列,
最后剩下的为大王.
静态链表的算法:
program statical_Monkey(input,output);
type component=record
data:integer;
cur:-1..10;
end;
var
a:array [1..10] of component;
i,p,k,b:integer;
deleted:boolean;
procedure DeleteLink(k:integer);
var
i,first,last:integer;
begin
for i:=1 to 10do
if a.cur=p then
first:=i;
last:=a[p].cur;
a[first].cur:=last;
a[p].cur:=-1;
p:=first;
end;
begin
for i:=1 to 10do
begin
a.data:=i;
a.cur:=i+1;
end;
a[10].cur:=1;
i:=1;
p:=1;
while a[p].cur<>pdo
begin
if i mod 3=0 then
begin
DeleteLink(p);
end;
k:=p;
p:=a[p].cur;
i:=i+1;
end;
write('The ');
write(a[k].data);
writeln(' (th) monkey will be the King');
end.
指针的算法
program Dynamic_Monkey(input,output);
type
pointer=^nodetype;
nodetype=record
data:integer;
linkointer;
end;
var
i,p,k,b:integer;
head,next,lastointer;
procedure typer(kointer);
var
i:integer;
k2ointer;
begin
i:=0;
k2:=k;
while k^.link<>k2do
begin
writeln(k^.data);
k:=k^.link;
i:=i+1;
end;
end;
procedure DeleteLink(var kointer);
var
tempointer;
begin
temp:=k;
while temp^.link<>kdo
temp:=temp^.link;
temp^.link:=k^.link;
write(k^.data);
write(' ');
dispose(k);
k:=temp;
end;
begin
new(head);
head^.data:=1;
last:=head;
for i:=2 to 10do
begin
new(next);
next^.data:=i;
last^.link:=next;
last:=next;
end;
last^.link:=head;
i:=1;
last:=head;
while last^.link<>lastdo
begin
if i mod 3=0 then
deletelink(last);
last:=last^.link;
i:=i+1;
end;
write('The ');
write(last^.data);
writeln(' (th) monkey will be the King');
end.
10个猴子,围成一圈,依次报数,从1到3, 1,2,3,1,2,3,1,2,3...
凡是数到3的就出列,
最后剩下的为大王.
静态链表的算法:
program statical_Monkey(input,output);
type component=record
data:integer;
cur:-1..10;
end;
var
a:array [1..10] of component;
i,p,k,b:integer;
deleted:boolean;
procedure DeleteLink(k:integer);
var
i,first,last:integer;
begin
for i:=1 to 10do
if a.cur=p then
first:=i;
last:=a[p].cur;
a[first].cur:=last;
a[p].cur:=-1;
p:=first;
end;
begin
for i:=1 to 10do
begin
a.data:=i;
a.cur:=i+1;
end;
a[10].cur:=1;
i:=1;
p:=1;
while a[p].cur<>pdo
begin
if i mod 3=0 then
begin
DeleteLink(p);
end;
k:=p;
p:=a[p].cur;
i:=i+1;
end;
write('The ');
write(a[k].data);
writeln(' (th) monkey will be the King');
end.
指针的算法
program Dynamic_Monkey(input,output);
type
pointer=^nodetype;
nodetype=record
data:integer;
linkointer;
end;
var
i,p,k,b:integer;
head,next,lastointer;
procedure typer(kointer);
var
i:integer;
k2ointer;
begin
i:=0;
k2:=k;
while k^.link<>k2do
begin
writeln(k^.data);
k:=k^.link;
i:=i+1;
end;
end;
procedure DeleteLink(var kointer);
var
tempointer;
begin
temp:=k;
while temp^.link<>kdo
temp:=temp^.link;
temp^.link:=k^.link;
write(k^.data);
write(' ');
dispose(k);
k:=temp;
end;
begin
new(head);
head^.data:=1;
last:=head;
for i:=2 to 10do
begin
new(next);
next^.data:=i;
last^.link:=next;
last:=next;
end;
last^.link:=head;
i:=1;
last:=head;
while last^.link<>lastdo
begin
if i mod 3=0 then
deletelink(last);
last:=last^.link;
i:=i+1;
end;
write('The ');
write(last^.data);
writeln(' (th) monkey will be the King');
end.