标签:color append def end 生成 斐波那契 col pre +=
# 方法一:
def fib(n): if n <= 2: return 1 else: return fib(n-1) + fib(n-2) for i in range(1,10): print(f‘fib({i}) = {fib(i)}‘)
# 方法二: def fab(max): n, a, b = 0, 0, 1 while n < max: print(b) a, b = b, a + b n += 1 fab(9)
# 方法二改良 def fab(max): n, a, b = 0, 0, 1 L = [] while n < max: L.append(b) a, b = b, a + b n += 1 return L fab(9)
# 方法一改良:使用生成器,节约内存空间 def fab(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n += 1 for n in fab(9): print(n)
标签:color append def end 生成 斐波那契 col pre +=
原文地址:https://www.cnblogs.com/wintalau/p/11622271.html