Jirairya

lintcode

2016-06-23

lintcode

斐波那契数列:

基本思路1:

def fibonacci(n):

    result=[]
    a, b = 0, 1
    result.append(a)
    result.append(b)
    while(n!=2):
        c=a+b
        a=b
        b=c
        n=n-1
        result.append(c)
    print result
    return result[-1]
if __name__=='__main__':
    n=int(raw_input(" input the number of list:"))
    print fibonacci(n)

基本思路2:

#-*- coding:utf-8 -*-
__author__ = 'l'
def fibonacci(n):
    fibonacci = [0,1]
    if n>1:
        for i in range(n-2):
            fibonacci.append(fibonacci[-2]+fibonacci[-1])
            self.value = fibonacci[-1]
        print self.value
        else:
            self.value = 0
            print self.value
num=input('The number:')
print fibonacci(num)

测试:

class Solution:
    # @param n: an integer
    # @return an integer f(n)
    def fibonacci(self, n):
        # write your code here
        #n=input("the number:")
        fibonacci = [0,1]
        if n>1:
            for i in range(n-2):
                fibonacci.append(fibonacci[-2]+fibonacci[-1])
            self.value = fibonacci[-1]
            print self.value
        else:
            self.value = 0
            print self.value
n=input("the number:")
Solution().fibonacci(n)

提交:

class Solution:
    # @param n: an integer
    # @return an integer f(n)
    def fibonacci(self, n):
        # write your code here
        fibonacci = [0,1]
        if n>1:
            for i in range(n-2):
                fibonacci.append(fibonacci[-2]+fibonacci[-1])
            self.value = fibonacci[-1]
            return self.value
        else:
            self.value = 0
            return self.value

待续。。。


上一篇 踩点

下一篇 reading-notes

Comments

Content