题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
java实现
public class Solution {
public int Fibonacci(int n) {
if(n<=0)
return 0;
if(n==1)
return 1;
int one=0;
int two=1;
int three=0;
for(int i=2;i<=n;i++)
{
three=two+one;
one=two;
two=three;
}
return three;
}
}
c++实现
class Solution {
public:
int Fibonacci(int n) {
if(n<=0)
return 0;
if(n==1)
return 1;
int one=0;
int two=1;
int three=0;
for(int i=2;i<=n;i++)
{
three=two+one;
one=two;
two=three;
}
return three;
}
};
python实现
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
if n is 0 or n is 1:
return n
a,b = 0,1
for i in range(n):
a,b = b,a+b
return a
# write code here