高精度阶乘

输入n,计算n的阶乘

例如:30!= 265252859812191058636308480000000

思路:利用数组保存计算结果

245*5    ==> 

5*5=25          5

4*5+2=22    2

2*5+2=12    2

0*5+1=1    1

结果为5221

    

//阶乘的精度值 
#include<iostream>
#include<cstring> 
#define MAX 3000
using namespace std;

int main()
{
    int n;
    cin >> n;
    int s[MAX + 1];     
    memset(s, 0, sizeof(s));     //将数组中的所有元素设为0,这步不能省、不能省、不能省 
    s[0] = 1;                   //初始值设为1 
    
    for(int i = 2; i <= n; i++)
    {
        int c = 0;
        for(int j = 0; j < MAX; j++)
        {        
            int t = s[j] * i + c;
            s[j] = t % 10;
            c = t / 10;    
            /*if(s[j+1] == 0 && c == 0)    //想想乘积中间很可能会有零,不能加这句。应该有更好的降低时间复杂度的方法 
                break;*/    
        }        
    }
    
    int m = MAX;
    while(!s[m] && m > 0) m--;
    for(; m >= 0; m--)
        cout << s[m];
    cout << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/fengyanlover/p/5292588.html