嘿嘿...有很多种方法...如果你觉得递归难理解的话...可以使用其它...
递归只是自身调用自身....有时使用递归....还是比较轻松简单的...
public static void main(String[] args){
System.out.println("循环不使用数组:" + finabocciLoop(7));
System.out.println("循环使用数组:" + finabocciArray(7));
System.out.println("使用递归方式:" + finabocciRec(7));
}
private static long finabocciLoop(int n){
int a = 1;
int b = 1;
int c = 0;
//c= a+b;
for(int i=2;i<n;i++){
c = a+b;
a = b;
b = c;
}
return c;
}
private static long finabocciArray(int n){
long[] finabo = new long[n];
finabo[0] =1;
finabo[1] =1;
for(int i=2;i<n;i++){
finabo[i] = finabo[i-1] + finabo[i-2];
}
return finabo[n-1];
}
private static long finabocciRec(int n){
long c;
if(n==1 || n==2)
c=1;
else
c=finabocciRec(n-1) + finabocciRec(n-2);
return c;
}