试用递归法编程计算菲波那切数列的通项飞f(n),已知f1 = 1, f2 = 1,以后每项都是前两项的和
package MonthSep.HWday04;
import java.util.Scanner;
public class HW02 {
public static int k = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long a = in.nextLong();
System.out.println(fibonacci(a));
System.out.println("共递归调用了" + k + "次");
}
public static long fibonacci(long m){
if(m == 0 || m == 1){
k++;
return m;
}else {
return fibonacci(m - 1) + fibonacci(m - 2);
}
}
}