2.2java语言基础——递归

?
?
?递归调用的程序分析:
calss Test
fibonacci sequence 斐波那契数列,使用非递归的方法。?
java文件:Fab.java
class="java" name="code">public class Fab {
public static void main(String[] args) {
System.out.println(f(-9));
}
public static long f(int index) {
if(index < 1) {
System.out.println("invalid parameter!");
return -1;
}
if(index == 1 || index == 2) {
return 1;
}
long f1 = 1L;
long f2 = 1L;
long f = 0;
for(int i=0; i<index-2; i++) {//index-2去掉前面的两个数
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
}
?
?