迭代器实现
class fb1(object):
def __init__(self,max):
self.max=max
self.a=0
self.b=0
self.n=0
def __iter__(self):
return self
def __next__(self):
if self.n<self.max
r = self.b
self.a,self.b=self.b,self.a+self.b
self.n+=1
return r
raise StopIteration
生成器实现
def fb2(max):
n,a,b=0
if n<max:
yield b
a,b=b,a+b
n+=1
for a in fb2(8):
print(a)
循环实现
def fb(max):
n,a,b=0,0,1
L =[]
while n<max:
L.append(b)
a,b=b,a+b
n+=1
return L
菲波那切数列
最新推荐文章于 2021-01-04 11:41:36 发布