public class demo01 {
public static void main(String[] args) {
for(int x=0;x<12;x++){
System.out.println(fun(x));
}
}
public static int fun(int i){
if(i==1 || i==2)
return 1;
else
return fun(i-1)+fun(i-2);
}
}

解决方案 »

  1.   

    从0开始啊         public static int fun(int i){
                    if(i==0 || i==1)
                        return 1;
                    else
                        return fun(i-1)+fun(i-2);
                }
      

  2.   

    Fibonacci数列啊,你的for循环从0开始,但是fun(0)=fun(-1)+fun(-2),这样会一直递归下去,当然最后StackOverflow
      

  3.   

    f(0)=f(-1)+f(-2)=f(-2)+f(-3)=f................
    死循环,
      

  4.   

    要注意递归的结束条件啊public static int fun(int i){
         if(i <= 1)
             return 1;
         else
             return fun(i-1)+fun(i-2);
    }
      

  5.   


    for (int x = 12; x > 0; x--) {
    System.out.println(fun(x));
    }
      

  6.   

    1.结束条件不对,楼上均正解
    2.递归算法不够好,Java栈保存了很多不必要的信息,数据量一大不是太慢就是会栈溢出。建议把递归改成尾递归进而改成迭代的形式,或者使用二阶矩阵二分法相乘的算法。(不过貌似这个算法的运行时间不比把当前递归换成迭代的形式,你写个性能测试的程序测试下)
      

  7.   

    package com.dai.check;public class Demo01 {
        public static void main(String[] args) {
            for(int x=12;x>0;x--){
                System.out.println(fun(x));
            }
        }
            public static int fun(int i){
                    if(i==1 || i==2)
                        return 1;
                    else
                        return fun(i-1)+fun(i-2);
                }
    }
      

  8.   

    建议使用动态规划, 不要使用递归
    int[] fun = new int[13];
    fun[1] = 1;
    fun[2] = 1;
    for (int i = 3; i < 13; i++){
         fun[i] = fun[i-1]+fun[i-2];
    }
    System.out.println(fun[12]);这样不但速度快,而却不可能出现内存溢出的情况