|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 asdererer 于 2016-4-14 13:35 编辑
鱼老师的:
- def fab(n):
- n1 = 1
- n2 = 1
- n3 = 1
- if n < 1:
- print('输入有误!')
- return -1
- while (n-2) > 0:
- n3 = n2 + n1
- n1 = n2
- n2 = n3
- n -= 1
-
- return n3
- result = fab(45)
- if result != -1:
- print('总共有%d对小兔崽子诞生!' % result)
复制代码
看别人的
- def fibonacci3(n):
- if n == 1 or n == 2:
- return 1
-
- # 用迭代进行计算
- nPre = 1
- nLast = 1
- nResult = 0
- i = 2
- while i < n:
- nResult = nPre + nLast
- nPre = nLast
- nLast = nResult
- i += 1
-
- return nResult
- result = fibonacci3(20)
- if result != -1:
- print('%d' % result)
复制代码
为啥别人 i += 1 鱼老师 n -=1 还都对。。。别人逻辑是啥?咋出来了个i |
|