斐波那契数列

0 1 2 3 4 5 6 7
0 1 1 2 3 5 8 13

 

 

  

 

 

 

Python实现:

 解一:

def Fibonacci(n):
    a = 0
    b = 1
    if n == 0:
        return a
    if n == 1:
        return b
l = [a, b] for i in range(2,n+1): l.append(l[i-1] + l[i-2]) return l[n]

解二:

def Fibonacci(n):
    a = 0
    b = 1
    if n == 0:
        return a
    if n == 1:
        return b

for i in range(2,n+1): c = a + b a = b b = c return b

解三:

def Fibonacci(self, n):
    if n == 0:
        return 0
    a = 0     # 第一个
    b = 1    # 第二个

for i in range(n): a, b = b, a+b return a

 

上一篇:Fibonacci POJ 3070


下一篇:2019年浙江省赛 I Fibonacci in the Pocket