高效率JAVA實現斐波那契

葉思維發表於2016-11-10

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);


}
}

相關文章