用递归的方法计算斐波那契数列的通项f(n),已知f1 = 1 ,f2 = 1,以后每项都是前两项的和。
public class fibonacci{
public static int k = 0;
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in){
long a = cin.nextLong();
System.out.println(fibonacci(a));
System.out.println("共递归调用了"+k+"次");
}
}
public static long fibcacci(long m){
if(m==0 ||m==1){
k++;
return m;
}else return fibonacci(m-1) + finbonacci(m-2);
}
}