求阶乘和(二次递归)

 1 public class test72 {
 2     /**
 3      * 求阶乘和
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         int sum = fac2(4);
 8         System.out.println(sum);
 9     }
10 
11     public static Integer fac1(int n) {
12         if(n == 1) {
13             return 1;
14         }
15           return n * fac1(n - 1);
16     }
17     public static Integer fac2(int n) {
18         int sum = 0;
19         for (int i = 1; i <= n; i ++) {
20             sum += fac1(i);
21         }
22         return sum;
23     }
24 }

 

posted @ 2020-04-26 20:58  听说在北郭  阅读(205)  评论(0)    收藏  举报