代码改变世界

高效率JAVA实现斐波那契

2016-11-10 10:19  叶思维  阅读(379)  评论(0编辑  收藏  举报

import java.util.Scanner;
public class Solution {
public static int Fibonacci(int n) {
int first = 0, second = 1,target=0;

if (n < 2)
return n ;
for (int i = 0; i < n-1; i++)
{
target = first + second;
first = second;
second = target;
}

return target;
}


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int num=Fibonacci(n);
System.out.println(""+num);


}
}