看看这个程序的输出结果是什么,能不能改进算f(n)的方法。 public class Squence { public static void main(String[] args) { System.out.println(fun(1,1,3)); } public static int fun(int A,int B,int n){ if(n==1 || n==2){ return n; } else{ return A*fun(A,B,(n-1))+B*fun(A,B,(n-2)); } }} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为什么这么写,只有你自己知道要想递归了多少次,可以统计一下public class Squence { static int count =0; public static void main(String[] args) { System.out.println(fun(1, 1, 3)); System.out.println("一共循环次数:"+count); } public static int fun(int A, int B, int n) { count++; if (n == 1 || n == 2) { return n; } else { return A * fun(A, B, (n - 1)) + B * fun(A, B, (n - 2)); } }} 求大侠给小妹指点迷津 一台机器同时登录多个用户session互相冲突如何解决? 求助:页面打开excel出现 乱码问题 struts图片显示问题 关于hibernate的问题 如何比较重复的数组成员并去掉重复的成员? 关于不同服务器间移植 J2EE程序开发中的src如何找到啊?(javax.servlet.*) EJB 错误 请问,哪里有RMI和EJB的书籍可以下载? 怎么样尽快精通java框架 用ssh2和jquery实现ajax交互,出现了点问题,求助,在线等待。。。。
要想递归了多少次,可以统计一下public class Squence {
static int count =0;
public static void main(String[] args) {
System.out.println(fun(1, 1, 3));
System.out.println("一共循环次数:"+count);
} public static int fun(int A, int B, int n) {
count++;
if (n == 1 || n == 2) {
return n;
} else {
return A * fun(A, B, (n - 1)) + B * fun(A, B, (n - 2));
}
}}