t = a //(saves 2 in t)
a = b //(now both a and b have same values)
b = t //(b gets the old value of a)
// now a = old value of b
// and b = old value of a.
瞧,变量互换了。你知道吗
好吧,那就是交换。在本规范中仅部分使用。最后一条语句b = t + b代码所做的是,将a的旧值与b相加(而不是替换它)。为什么?将前面的2相加,得到斐波那契数列中的下一个数。你知道吗
假设a=2,b=3。你知道吗
关键是第二部分。
t = a
表示t
获得与a
相同的值。这并不意味着t
和a
现在都是一回事。你知道吗您可以在Python提示符中尝试:
这叫做变量交换。如何替换变量的值?你知道吗
正如@smarx所说,当
a = 2
和b = 3
时,如何使之a = 3
和b = 2
?你知道吗当您执行
a = 3
时,a(2)的旧值丢失,因此您不知道用什么来设置b。所以我们把它存储在一个临时变量(t)中。你知道吗所以
瞧,变量互换了。你知道吗
好吧,那就是交换。在本规范中仅部分使用。最后一条语句
b = t + b
代码所做的是,将a的旧值与b相加(而不是替换它)。为什么?将前面的2相加,得到斐波那契数列中的下一个数。你知道吗2,3,5是斐波那契序列,因为5=2+3(给定2和3是种子值)。这正是这段代码要做的。你知道吗
在你的第一次跑步中
在你的第二次跑步中
在你的第三次跑步中
在你的第四次跑步中
等等。。。你知道吗
相关问题 更多 >
编程相关推荐