参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); long []arr = new long[n + 1]; long sum = 0; for(int i = 1; i <= n; i++) { sum += factorial(arr, i); } System.out.print(sum); } public static long factorial(long []arr, int n) { if(arr[n] != 0) return arr[n]; else if(n <= 1) { arr[n] = 1; return arr[n]; } arr[n] = factorial(arr, n - 1) * n; return arr[n]; } }
0.0分
6 人评分