📜  使用动态编程方法的斐波那契数列 - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:24.907000             🧑  作者: Mango

代码示例1
def fib(n, solved):
 
    # Base case
    if n == 0 or n == 1 :
        solved[n] = n
 
    # If the value is not calculated yet then calculate it
    if solved[n] is None:
        solved[n] = fib(n-1, solved)+fib(n-2, solved)
 
    # return the value from table for n
    return solved[n]

n = 12
solved = [None]*(n+1)
fib(n, solved)
for i, num in enumerate(solved):
    print(i, num)