修饰器,增加缓存的意义
4.如何用Python输出一个Fibonacci数列?
1 a,b = 0, 1 2 while b<100: 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<=1:
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 /]#
阅读(7576) | 评论(0) | 转发(0) |