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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值