首页 > 编程语言 > 详细

Python 输出斐波纳挈数列

时间:2015-04-15 09:46:14      阅读:271      评论:0      收藏:0      [点我收藏+]
#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()


结果:

技术分享

Python 输出斐波纳挈数列

原文:http://blog.csdn.net/tb6013245/article/details/45047829

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!