2 条题解

  • 1
    @ 2026-6-2 20:07:31

    这题思路很简单

    只需要根据题目就能看出,求的就是1! + 2! + 3! + ... + n!.

    而n!就是1 * 2 * 3 * ... * n.

    那么就可以写两层循环,第一层为从1~n,第二层为1~i,然后使用一个 cnt数组去存第二次循环所以数的积,在第二层循环之后,使用一个ans数组去存所有cnt相加的和。

    代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int n, ans = 0;
    
    int main() {
    	cin >> n;
    	for (int i = 1; i <= n; i++) {
    		int cnt = 1;
    		for (int j = 1; j <= i; j++) {
    			cnt *= j;
    		}
    		ans += cnt;
    	}
    	cout << ans ;
    
    	return 0;
    }
    
    
    
    
    • 0
      @ 2026-5-30 11:26:51

      #include using namespace std;

      int main() { int n; cin >> n; int total = 0; int factorial = 1; for (int i = 1; i <= n; i++) { factorial *= i; total += factorial; } cout << total << endl; return 0; }

      • @ 2026-5-31 8:28:48

        在所有的“;”后面回车就行

    • 1

    信息

    ID
    30023
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    4
    上传者