50分的高精,新手求助QAQ

P1009 [NOIP1998 普及组] 阶乘之和

Anzhitinglan @ 2023-09-23 11:59:14

#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>

using namespace std;

string a;
int sum[99999999] , len , b[99999999] , sumfin[99999999];

void fac(int n)
{
    int total = 1 ;
    for(int i = 1 ; i <= n ; i++) total *= i;
    a = to_string(total) ;
    len = a.length() ;
    for( int i = len -1 ; i >= 0 ; i-- ) b[len - i ] = a[i] - '0' ; 
}

int main()
{
    int n , lenmax = 0;
    cin >> n ;
    for(int i = 1 ; i <= n ; i++ )
    {
        fac(i) ;
        lenmax = max(len, lenmax) ;
        for(int i = 1 ; i <= lenmax ; i++ )
        {
            sum[i] += b[i];
            sum[i+1] += sum[i] / 10 ;
            sum[i] %= 10 ;
        }
        //if(sum[lenmax+1]) lenmax++;
        while(sum[lenmax+1]) lenmax++;
    }
    for(int i = lenmax ; i >= 1 ; i-- ) cout << sum[i] ;

    return 0;
}

by Wind001 @ 2023-10-04 16:15:12

你这个当n比较大时,函数里面total范围就已经越了,后面再转成string也没有意义了。


|