动态规划--斐波那契数列

博客地址:https://www.cnblogs.com/zylyehuo/

# -*- coding: utf-8 -*-


# 子问题的重复计算--递归方法--执行效率低
def fibnacci(n):
    if n == 1 or n == 2:
        return 1
    else:
        return fibnacci(n - 1) + fibnacci(n - 2)


# print(fibnacci(100))


# 动态规划(DP)的思想 = 递推式 + 重复子问题
def fibnacci_no_recurision(n):
    f = [0, 1, 1]
    if n > 2:
        for i in range(n - 2):
            num = f[-1] + f[-2]
            f.append(num)
    return f[n]


print(fibnacci_no_recurision(100))

posted @ 2023-08-19 12:44  zylyehuo  阅读(12)  评论(0编辑  收藏  举报