7-5 计算阶乘和

题解:两重循环,内层循环求阶乘的值,外层求和。 时间复杂度 O(n^2) 空间复杂度 O(n)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans,n;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        ll t = 1;
        for(int j = 1; j <= i; j++)
            t *= j;
        ans += t;
    }
    std::cout<<ans<<'
';
    return 0;
}

  

原文地址:https://www.cnblogs.com/Edviv/p/12254393.html