题解 | #阶乘#

阶乘

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)

全部评论

相关推荐

狠赚笔第一人:学计算机自己不努力怪大环境?我大一就拿到了美团大厂的offer,好好看看自己有没有努力查看图片
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务