首页 > 代码库 > Hdu 1042 N! (高精度数)

Hdu 1042 N! (高精度数)


Problem Description

Givenan integer N(0 ≤ N ≤ 10000), your task is to calculate N!

 

 

Input

OneN in one line, process to the end of file.

 

 

Output

Foreach N, output N! in one line.

 

 

Sample Input

1

2

3

 

 

Sample Output

1

2

6

 

 

Author

JGShining(极光炫影)

 

/*********

高精度数,大数阶乘

类比十进制,模拟一个万进制的算法算法,参考:http://blog.csdn.net/lulipeng_cpp/article/details/7437641

用int 存数位,10000*100000<2^31,所以我每一位存了 100000,当然与 10000是类似的

*********/

Code:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
/**
计算阶乘位数,顺便把POJ 1423 给A了
#define PI 3.141592653589793239
#define ee 2.7182818284590452354
int ans(int n){
    return (int)((n*log10(n/ee)+log10(sqrt(2*n*PI))))+1;
}

*/
int num[8000];
int main()
{
    int n,i,j;
    while(cin>>n)
    {
        memset(num,0,sizeof(num));
        num[0] = 1;
        for(i = 2;i<=n;i++)
        {
            for(j = 0;j<8000;j++)
                num[j]*=i;
            for(j = 0;j<8000;j++)
            {
                num[j+1] += num[j]/100000;
                num[j] %= 100000;
            }
        }
        int len = 8000;
        while(num[len] == 0)
        {
            len--;
        }
        cout<<num[len];
        for(i = len-1;i>=0;i--)
        {
            printf("%.5d",num[i]);
        }
        printf("\n");
    }
    return 0;

}


Hdu 1042 N! (高精度数)