Python_每日习题_0006_斐波那契数列

程序设计:

  斐波那契数列(Fibonacci sequence),从1,1开始,后面的每一项等于前面两项之和。

图方便就递归实现,图性能就用循环。

# for 循环

target = int(input())
res = 0
a,b =1,1

for i in range(target-1):
    a, b=b, a+b

print(a)

#a,b=b,a+b是先计算等号右边,右边计算完成再依次赋值给左边。
# 递归实现:

def Fib(n):
    return 1 if n<=2 else Fib(n-1) + Fib(n-2)

print(Fib(int(input())))
原文地址:https://www.cnblogs.com/LXL616/p/10689706.html