主题:[讨论]斐波那契问题
905482852
[专家分:380] 发布于 2009-06-28 13:54:00
斐波那契问题:1,1,2,3,5,8,13..........N求第N个数是几?
输入:1;输出:1
输入:7;输出:13
请问哪位朋友能编出来?
事先说明一下我知道怎么编啊!
回复列表 (共2个回复)
沙发
1054122 [专家分:750] 发布于 2009-06-28 18:33:00
CLS
INPUT n
DIM a(n + 1)
a(1) = 1: a(2) = 1
FOR i = 3 TO n STEP 1
a(i) = a(i - 1) + a(i - 2)
NEXT i
PRINT a(n)
END
板凳
我爱qbasic [专家分:160] 发布于 2009-07-12 17:34:00
【原创]
请勿抄袭!!!
方法一(普通算法):
CLS
INPUT n
DIM a(n)
a(1) = 1: a(2) = 1
FOR i = 3 TO n
a(i) = a(i - 1) + a(i - 2)
NEXT i
PRINT a(n)
END
方法2(递归算法):
cls
input n
print fib(n)
end
function fib(x)
if n=1 then
fib=1
else
if n=2 then
fib=2
else
fib=fib(n-1)+fib(n-2)
endif
end function
方法3(高精度):
CLS
INPUT "n=", n
DIM a(200), b(200), c(201), s(201)
a(1) = 1
b(1) = 1
s(1) = 2
FOR i = 3 TO n
jw = 0
FOR j = 1 TO 200
c(j) = a(j) + b(j) + jw
jw = c(j) \ 10
c(j) = c(j) MOD 10
NEXT
FOR j = 1 TO 200
a(j) = b(j)
b(j) = c(j)
NEXT
NEXT
i = 201
DO WHILE c(i) = 0
i = i - 1
LOOP
y = 201
DO WHILE s(y) = 0
y = y - 1
LOOP
PRINT "di n xiang wei:";
FOR j = i TO 1 STEP -1
PRINT USING "#"; c(j);
NEXT
PRINT
我来回复