15: factorial and

Portal: http: //noi.openjudge.cn/ch0106/15/

 

 

Calculated with high accuracy S = 1! +2! +3! + ... + n! (N≤50)

"!" Denotes factorial wherein, for example:! 5 4 = 5 * 3 * 2 * 1 *.

Enter a positive integer N, outputs the calculation result S.

Entry
A positive integer N.
Export
The results S.
Sample input
5
Sample Output
153



#include<iostream>
using namespace std;
#define N 100000
int a[N],n,tot=1,lena=1,ans[N];
int main()
{
    cin>>n;
    a[1]=1;ans[1]=1;
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=lena;j++)a[j]*=i;
        for(int j=1;j<=lena;j++)
        {
            if(a[j]>9)
            {
                a[j+1]+=a[j]/10;
                a[j]%=10;
                if(j==lena)lena++;
            }
        }
        tot=max(lena,tot);
        for(int j=1;j<=tot;j++)
            ans[j]+=a[j];
        for(int j=1;j<=tot;j++)
            if(years [j]> 9 ) 
            { 
                years [j + 1 ] ++ ; 
                years [j] - = 10 ;
                if (tot == j) tot ++ ; 
            } 
    } 
    // cout << endl << tot; 
    for ( int i = tot; i> = 1 ; i -) cout << years [i]; 
    cout << endl; 
}

 

Guess you like

Origin www.cnblogs.com/jzxnl/p/11105558.html