计算1!+2!+3!+…+n!

image

1.

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int tmp = 1, ans = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= i; j ++)
        {
            tmp *= j;
        }
        ans += tmp;
        tmp = 1;
    }
    cout << ans << endl;
    
    return 0;
}

2.

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    long long tmp = 1, ans = 0;
    for (int i = 1; i <= n; i ++)
    {
        tmp *= i;
        ans += tmp;
    }
    cout << ans << endl;
    
    return 0;
}
posted on 2023-05-23 19:37  逆袭怪  阅读(8)  评论(0编辑  收藏  举报