请补充以下代码:def fib(n): a = 0 b = 1 dic = _____ for i in _____: dic[i]=_____ a,b=b,a+b return ______ fibonac = ______ for key in fibonac: print(fibonac[key], end=",")
时间: 2023-05-21 14:04:56 浏览: 125
答案:
def fib(n): a = 0 b = 1 dic = {} for i in range(n): dic[i]=a a,b=b,a+b return dic fibonac = fib(10) for key in fibonac: print(fibonac[key], end=",")
输出结果为:0,1,1,2,3,5,8,13,21,34,
相关问题
请补充一下代码:def fib(n): a = 0 b = 1 dic = ___(1)___ for i in ___(2)___: dic[i]=___(3)___ a,b=b,a+b return ___(4)___ fibonac = ___(5)___ for key in fibonac: print(fibonac[key], end=",")
def fib(n):
a = 0
b = 1
dic = {1: a, 2: b}
for i in range(3, n+1):
dic[i]=a+b
a,b=b,a+b
return dic
fibonacci = fib(10)
for key in fibonacci:
print(fibonacci[key], end=",")
请补充下面的代码def fib(n): a = 0 b = 1 dic = ___(1)___ #定义字典 for i in ___(2)___: dic[i]=___(3)___ #把添加斐波那契数到字典 a,b=b,a+b return ___(4)___ #调用函数生成斐波那契数列中的前20个斐波那契数 fibonac = ___(5)___ for key in fibonac: print(fibonac[key], end=",")
请补充下面的代码:
def fib(n):
a = 0
b = 1
dic = {0:0, 1:1} # 表示斐波那契数列的前两项
...
代码中应该补充出现超过前两项的斐波那契数列项数的字典值,可以优化斐波那契数列运算效率,避免重复计算。
阅读全文