斐波那契数列指的是这样一个数列:1、1、2、3、5、8....输出前 N 个 斐波那契数,要求每行5个

思路:理解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, 1
    i=0#判断值
    while i<=n:
        a, b = b, a + b
        print(a,end=" ")#数字间以空格隔开
        i+=1
        if i%5==0:#每5个换行
            print(end="\n")
        if i==n:#只输出n个数
            break
Fibonacci(10)

猜你喜欢

转载自blog.csdn.net/winds_tide/article/details/106686242