阶乘和

 

 

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n;
int a[maxn] = {0,1},s[maxn];
int num = 1,alen = 1,jin = 1,slen = 1;
int i,j;
int main(){
    ios::sync_with_stdio(0);
    cin >> n;
    for(i = 1; i <= n; i++){
        //乘法
        for(j = 1; j <= alen; j++)
            a[j] *= num;
        for(j = 1; j <= alen; j++) {
            if (a[j] >= 10) {
                a[j + 1] += a[j] / 10;
                a[j] %= 10;
                jin++;
            }
        }
        alen++;
        while(a[alen] >= 10){
            a[alen + 1] = a[alen] / 10;
            a[alen] %= 10;
            alen++;
            jin++;
        }
        num++;
        //加法
        slen = max(slen,alen);
        for(j = 1; j <= slen; j++){
            s[j] += a[j];
            if(s[j] >= 10){
                if(j == slen) slen++;
                s[j + 1] += s[j] / 10;
                s[j] %= 10;
            }
        }
        alen = jin;
    }
    //去除前导0
    for( i = slen; i > 1; i--){
        if(s[i])
            break;
    }
    for(j = i; j>= 1; j--)
        cout << s[j];
    return 0;
}

 

posted @ 2020-03-25 07:59  Hazelxcf  阅读(386)  评论(0编辑  收藏  举报