递归

递归

image

image

image

image
image

package com.luo.method;

public class Demo5 {
    public static void main(String[] args) {
        Demo5 test = new Demo5();
        test.test();

    }


    public void test(){
        test();
    }
}
package com.luo.method;

public class Demo6 {
    //小计算可用递归解决。大计算用递归容易令电脑崩溃,原因:栈压缩太多,电脑物理条件溢出。
    /*
       阶乘
       表示符:!
       案例:2! 2*1
       3!  3*2*1
       5!  5*4*3*2*1
        */
    public static void main(String[] args) {
        System.out.println(f(5));
    }

    //5! 5*4*3*2*1
    public static int f(int n){

        if (n==1){
            return 1;
        }else{
            return n*f(n-1);
        }
    }
}
posted @ 2021-05-13 08:55  南雄  阅读(37)  评论(0)    收藏  举报