哦!
递归程序
program Stair;
var
x:integer;
function Calc(x:integer):integer;
begin
case x of
1:begin Calc:=1; exit end;
2:begin Calc:=2; exit end;
3:begin Calc:=4; exit end;
else Calc:=Calc(x-1)+Calc(x-2)+Calc(x-3);
end
end;
begin
readln(x);
writeln(Calc(x);
end.
当然,此算法极有可能超时,所以可用递推法(类似于动态规划)
program Stair;
var
x,i:integer;
a:array[1..1000]of integer;
begin
readln(x);
a[1]:=1; a[2]:=2; a[3]:=4;
if x<4 then begin writeln(a[x]); halt end;
for i:=4 to x do
a:=a[i-1]+a[i-2]+a[i-3];
writeln(a[x])
end.
这样就不会超时了!