递归求n的阶乘

sullay

关注

阅读 191

2022-03-19

递归的必要条件:

1. 将原问题划分成其子问题,注意:子问题必须要与原问题的解法相同

2. 递归出口

public class TestMethod {
    //通过递归求n的阶乘
    public static int factor(int n) {
        if (n == 1) {
            return 1;
        }
        return n * factor(n - 1);

    }

    public static void main(String[] args) {
        int n=5;
        int ret=factor(n);
        System.out.println("ret="+ret);
    }
}

精彩评论(0)

0 0 举报