1 1 2 3 5 8 13 21 34 ..........
方法一:
public class Recursion {
/**
* @param args
*/
public static void main(String[] args) {
Recursion r = new Recursion();
r.f(30);
System.out.println(r.f(1));
System.out.println(r.f(3));
System.out.println(r.f(5));
System.out.println(r.f(30));
}
public long f(long i){
if(i==1 || i==2){
return 1;
}
else{
return f(i-1)+f(i-2);
}
}
}
方法二:
public class Recursion {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(f(15));
}
static int f(int n){
return n==1||n==2?1:f(n-1)+f(n-2);
}
}