Python 输出斐波纳挈数列
发布时间:2020-05-29 00:06:04 所属栏目:Python 来源:互联网
导读:Python 输出斐波纳挈数列
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #coding:utf8
import time
import os
global_list = []
def test1(n):
if n==1 or n==2:
return 1
else:
return test1(n-1) + test1(n-2)
def test2(n):
global global_list
if n==1 or n==2:
return 1
else:
temp = test2(n-1) + test2(n-2)
if isinstance(temp,int):
global_list.append(temp)
return temp
def main():
global global_list
input_int = raw_input("Entry your number:")
length = int(input_int)
#第一种方法
start = time.clock()
for i in xrange(length):
print test1(i+1
),end = time.clock()
print os.linesep + "处理耗时:%f s"%(end-start)
#第二种方法
start = time.clock()
test2(length)
temp_set = set(global_list)
global_list = [i for i in temp_set]
global_list.sort(reverse=True)
global_list.extend([1,1])
for i in global_list[::-1]:
print i,end = time.clock()
print os.linesep + "处理耗时:%f s"%(end-start)
if __name__ == "__main__":
main()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
