看看这个程序的输出结果是什么,能不能改进算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)); } }} SSH整合错误,求大神指点 大数据量时 dtree加载极其慢 JSP 数据查询之前怎么显示“数据正在加载中”? 高手请进,关于java抓取别人的页面来 求java.util.timer 定时器嵌套用法 hibernate第一次使用就出问题 百分求教,如何用UDP模拟SOCKET? 大家进来帮帮忙~ jboss中配置cmp的困惑(cmp映射到另一个表中去了)???? weblogic及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));
}
}}