主题:紧急紧急!帮者加高分!!!!!!
54321
[专家分:20] 发布于 2006-01-31 13:55:00
[b]楼梯有n阶台阶,上楼可以一步一个台阶,也可以一步两个台阶。问有几种走法。(递归与非递归)[/b]
回复列表 (共4个回复)
沙发
小田甜 [专家分:3910] 发布于 2006-01-31 14:44:00
n 走法
1 1
2 2
3 3
4 5
5 8
.
.
.
program xtt;
var
n,i:byte;
j1,j2,j3:string;
function h(a,b:string):string;
var i:byte;c:string;
begin
c:='';
{-5-} if length(a)<length(b)
then for i:=1 to length(b)-length(a) do insert('0',a,1)
else for i:=1 to length(a)-length(b) do insert('0',b,1);{对齐数位}
{-6-} for i:=length(a) downto 0 do
if (i=0) then begin if (ord(a[1])+ord(b[1])>=106) then insert('1',c,1) end
else
if i=length(a) then
insert(chr((ord(a[i])+ord(b[i])-96) mod 10+48),c,1)
else
insert(chr((ord(a[i])+ord(b[i])-96+
ord(ord(a[i+1])+ord(b[i+1])>=106)) mod 10+48),c,1);{加法核心}
h:=c;
end;
begin
readln(n);
j1:='1';j2:='1';j3:='1';
for i:=2 to n do begin
j3:=h(j1,j2);
j2:=j1;j1:=j3;
end;
write(j3);readln;
end.
板凳
小田甜 [专家分:3910] 发布于 2006-01-31 14:46:00
附高精度加法:
{ program xtt;}
var{申请变量}
{-1-} a,b,h:string;
{-2-} i:byte;
begin{main}
{-3-} readln(a);{读入}
{-4-} readln(b);
{-5-} if length(a)<length(b)
then for i:=1 to length(b)-length(a) do insert('0',a,1)
else for i:=1 to length(a)-length(b) do insert('0',b,1);{对齐数位}
{-6-} for i:=length(a) downto 0 do
if (i=0) then begin if (ord(a[1])+ord(b[1])>=106) then insert('1',h,1) end
else
if i=length(a) then
insert(chr((ord(a[i])+ord(b[i])-96) mod 10+48),h,1)
else
insert(chr((ord(a[i])+ord(b[i])-96+
ord(ord(a[i+1])+ord(b[i+1])>=106)) mod 10+48),h,1);{加法核心}
{-7-}writeln(h);{打印}
end.
3 楼
小田甜 [专家分:3910] 发布于 2006-01-31 14:51:00
非高精度解法:
program xtt;
var
n,i,j1,j2,j3:word;
begin
readln(n);
j1:=1;j2:=1;j3:=1;
for i:=2 to n do begin
j3:=j1+j2;
j2:=j1;j1:=j3;
end;
write(j3);readln;
end.
4 楼
小田甜 [专家分:3910] 发布于 2006-01-31 14:58:00
递归解法:
program xtt;
var n:byte;
function try(n:byte):word;
begin
if n<=2 then try:=n else try:=try(n-1)+try(n-2);
end;
begin
readln(n);
write(try(n));
readln;
end.
我来回复