斐波那契数列_java版本
package 斐波那契数列;
public class fbnq {
public static void main(String[] args){
System.out.println(fibonacci(10));
}
// 递归实现方式
public static int fibonacci(int n){
if(n <= 2){
return 1;
}else{
return fibonacci(n-1) + fibonacci(n-2);
}
}
// 递推实现方式
public static int fibonacciNormal(int n){
if(n <= 2){
return 1;
}
int n1 = 1, n2 = 1, sn = 0;
for(int i = 0; i < n - 2; i ++){
sn = n1 + n2;
n2 = sn;
}
return sn;
}
}
-----------------------------------------------------
package 斐波那契数列;
public class fbnq {
public static void main(String[] args){
System.out.println(f(10));
}
public static int f(int a){
if(a<=2) return 1;
else{
return f(a-1)+f(a-2);
}
}
}