递归、回溯练习题答案

一、
var x,y:integer;
function acm(x,y:integer):integer;
begin
if x=0 then exit(y+1)
else if y=0 then exit(acm(x-1,1))
else exit(acm(x-1,acm(x,y-1)))
end;
begin
readln(x,y);
writeln(acm(x,y))
end.

二、
var n,m:integer;
function zuhe(n,m:integer):integer;
begin
if m=1 then exit(n)
else if n=m then exit(1)
else exit(zuhe(n-1,m)+zuhe(n-1,m-1))
end;
begin
readln(n,m);
writeln(zuhe(n,m))
end.

三、
var s:string;
procedure pl(s:string);
var i:integer;
c:string;
begin
if length(s)=5 then writeln(s)
else for i:=1 to 5 do
begin
str(i,c);
if pos(c,s)=0 then pl(s+c)
end;
end;
begin
pl(”)
end.

四、
var n:longint;
procedure fg(n:longint);
var t:integer;
begin
if n<>0 then
begin
t:=n mod 10;
fg(n div 10);
write(t,’ ‘)
end;
end;
begin
readln(n);
fg(n)
end.

五、
var n,sum,bs:integer;
x:array[1..100] of integer;
procedure try(i:integer);
var j,t:integer;
begin
if i=n then
(begin 可以省略)
inc(sum)(;去掉分号) (for t:=1 to bs do write(x[t],’ ‘); writeln; 可以省略)
(end 可以省略)
else for j:=1 to 3 do
if i+j<=n then
begin
inc(bs);
x[bs]:=i;
try(i+j);
dec(bs);
end;
end;
begin
readln(n);

你可能感兴趣的:(回溯,递归)