主题:火车进栈问题
问题描述:编号为1,2,……,n的n辆列车顺序进入一个栈式结构的站台。试给出这n辆列车开出车站的所有可能次序。
var
s:array[1..100]of integer;
a:array[1..100]of integer;
i,j,k,l,m,n:integer;
procedure serch(k{n},m{a},i{s}:integer);
var j,temp:integer;
begin
if m=n then begin
for j:=1 to n do write(output,a[j],' ');
writeln;
exit;
end;{full}
if k<>n+1 then begin temp:=s[i+1];
s[i+1]:=k;
serch(k+1,m,i+1);
s[i+1]:=temp;end;{in}
if i<>0 then begin
temp:=a[m+1];
a[m+1]:=s[i];
serch(k,m+1,i-1);
a[m+1]:=temp;
end;{out}
end;
begin
assign(input,'d:\work.in');
reset(input);
assign(output,'d:\work.out');
rewrite(output);
read(input,n);
serch(1,0,0);
close(input);
close(output);
end.
此帖转自:[url]http://www.programfan.com/team/team.asp?team_id=1346[/url]
var
s:array[1..100]of integer;
a:array[1..100]of integer;
i,j,k,l,m,n:integer;
procedure serch(k{n},m{a},i{s}:integer);
var j,temp:integer;
begin
if m=n then begin
for j:=1 to n do write(output,a[j],' ');
writeln;
exit;
end;{full}
if k<>n+1 then begin temp:=s[i+1];
s[i+1]:=k;
serch(k+1,m,i+1);
s[i+1]:=temp;end;{in}
if i<>0 then begin
temp:=a[m+1];
a[m+1]:=s[i];
serch(k,m+1,i-1);
a[m+1]:=temp;
end;{out}
end;
begin
assign(input,'d:\work.in');
reset(input);
assign(output,'d:\work.out');
rewrite(output);
read(input,n);
serch(1,0,0);
close(input);
close(output);
end.
此帖转自:[url]http://www.programfan.com/team/team.asp?team_id=1346[/url]