Python 输出斐波纳挈数列
2018-07-20 来源:open-open
#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()
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:C#实现的客户端弹出消息框封装类
最新资讯
热门推荐