专业建站流程,安防网站模板,泉州手工外发加工网,一个新品牌如何推广计算斐波那契数列第n项的数字
Description计算斐波那契数列第n项的数字#xff0c;其中f(1)f(2)1,f(n)f(n-1)f(n-2)#xff0c;如1#xff0c;1#xff0c;2#xff0c;3#xff0c;5,......Input
正整数n(n100)Output
一个整数f(n)Sample Input 1 8
Sample Output 1…计算斐波那契数列第n项的数字
Description计算斐波那契数列第n项的数字其中f(1)f(2)1,f(n)f(n-1)f(n-2)如11235,......Input
正整数n(n100)Output
一个整数f(n)Sample Input 1 8
Sample Output 121
Source中学生可以这样学PythonP187例8-19一、使用递归出现超时
nint(input())
def f(n):if n1 or n2:return 1else:return f(n-1)f(n-2)
print(f(n))二、使用列表通过
nint(input())
a[1,1]
if n1 or n2:print(1)
else:for i in range(2,n):a.append(a[i-2]a[i-1])
print(a[n-1])