大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
n<=39
斐波那契数列,又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)
public class Solution {
public int Fibonacci(int n) {
if(n<=1)
{
return n;
//ystem.out.println("n");
}else {
int temp0=0;
int temp1=1;
int temp=0;
for(int i=2;i<n+1;i++)
{
temp=temp1+temp0;
temp0=temp1;
temp1=temp;
}
// System.out.println("temp");
return temp;
}
}
}