作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Python 文档:
>>> def fib(n):
a,b=0,1
while a<n:
print a,
a,b=b,a+b
>>> fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
我的重新实现:
>>> def fib2(n):
a=0
b=1
while a<n:
print a
a=b
b=a+b
>>> fib2(2000)
0
1
2
4
8
16
32
64
128
256
512
1024
最佳答案
两者的区别
a,b=b,a+b
和
a=b
b=a+b
是在第二个中,a
被赋值为b
,然后b
被赋值为a的和
和 b
,表示它是原始值的两倍。
考虑:
a = 1
b = 2
a,b = b,a+b
这给出了
a==2 (old value of b)
b==3 (sum of 1 and 2)
反之:
a = 1
b = 2
a = b
b = a + b
给出:
a==2 (old value of b)
b==a+b==2+2==4 (twice the old value of b)
关于python - python 文档中的这段斐波那契代码与我的重新实现相比有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27345831/
我是一名优秀的程序员,十分优秀!