修饰器,增加缓存的意义
4.如何用Python输出一个Fibonacci数列?
1 a,b = 0, 1 2 while b: 3 print (b), 4 a, b = b, a+b

[root@localhost /]#
[root@localhost /]# cat zlg.py
#!/bin/python
def a(n,cache=None):
    if cache is None:
        cache={}
    if n in cache:
        return cache[n]
        print n
    if n        return 1
    cache[n]=a(n-1,cache)+a(n-2,cache)
#    print n
#    print '**********'
#    print cache[n]
    return cache[n]
print a(5)
[root@localhost /]#

10-04 16:28
查看更多