思路:理解Fn=F(n-1)+F(n-2)(n>=2,n∈N*)并运用。
换成Python语言就是F(n-1),F(n-2)=F(n-2),F(n-1)+F(n-2)
第一个数等于第二个数,而第二个数等于两者之和,重复运用。
同时设立一个判断值,完成输出前 N 个 斐波那契数,要求每行5个的任务。
def Fibonacci(n):a, b = 0, 1i=0#判断值while i<=n:a, b = b, a + bprint(a,end=" ")#数字间以空格隔开i+=1if i%5==0:#每5个换行print(end="\n")if i==n:#只输出n个数breakFibonacci(10)