题目 求n!得结果
【暴力递归,将问题转化为规模缩小了的同类问题的子问题】
package algorithm.section8;
public class Factorial {
public static long getFactorial1(int n) {
if (n == 1) return (long)n;
return n * getFactorial1(n - 1);
}
public static long getFactorial2(int n) {
long res = 1;
for (int i = 1; i <= n; i++)
res *= i;
return res;
}
public static void main(String[] args) {
System.out.println(getFactorial1(4));
System.out.println(getFactorial2(4));
}
}