HDU&&NYoj N!

One N in one line, process to the end of file.

Output
For each N, output N! in one line.

Sample Input
  
  
1 2 3

Sample Output
  
  
1 2 6
描述
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
输入
输入一个整数m(0<m<=5000)
输出
输出m的阶乘,并在输出结束之后输入一个换行符
样例输入
50
样例输出
30414093201713378043612608166064768844377641568960512000000000000


#include <cstdio>
#include <cstring>
using namespace std;

int main ()
{
    int a[9000];
    int  tmp, n, c, i, j, d;
    while (scanf("%d",&n)!=EOF)
    {
        a[1]=1;
        d=1;
        for(i=2; i<=n; ++i)
        {
            for(j=1,c=0; j<=d; ++j)
            {
                tmp=a[j]*i+c;
                a[j]=tmp%10000;//当a[j]>4位数的时候,把第一个数拿出来;
                c=tmp/10000;
            }
            while(c>0)
            {
                d++;
                a[d]=c%10000;
                c=c/10000;
            }

        }
        printf("%d",a[d]);//输出第一个数;
        //printf("%d",a[d]);
        for(j=d-1; j>=1; --j)
        {
            printf("%04d",a[j]);//输出除了第一个数之后的数,如果不是4位数,则补零;
        }
        printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值