题解 | #阶乘#
阶乘
https://www.nowcoder.com/practice/e58c8a55162d49c48115bdfa5da7da56
//Java版代码 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] factorials = new int[n + 1]; factorials[0] = 1; for (int i = 1; i <= n; i++) factorials[i] = factorials[i - 1] * i; int sum1 = 0, sum2 = 0; for (int i = 1; i <= n; i += 2) sum1 += factorials[i]; for (int i = 2; i <= n; i += 2) sum2 += factorials[i]; System.out.println(sum1 + " " + sum2); } } //Python版代码 from math import factorial sum1 = sum2 = 0 n = int(input()) for i in range(1, n + 1, 2): sum1 += factorial(i) for i in range(2, n + 1, 2): sum2 += factorial(i) print(sum1, sum2)