码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1042 N!

时间:2015-09-30 12:38:40      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

/*
N!
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 67077    Accepted Submission(s): 19228


Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!


Input
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



Author
JGShining(极光炫影)


Recommend
We have carefully selected several similar problems for you:  1715 1063 1753 1316 1013
*/
#include <iostream>
#include<stdio.h>
using namespace std;
const int Len = 40000;
int *a =new int[Len];
int main()
{
    int i,j,n,pos,over;
    //double test;
    while(scanf("%d",&n)!=EOF)
    {
        //test=1;
        pos=0;
        a[0]=1;
        for(i=1; i<=n; i++)
        {
            over=0;
            for(j=0; j<=pos; j++)
            {
                a[j]*=i;
                a[j]+=over;
                over=a[j]/100000;//each five bit calculate
                a[j]%=100000;
            }
            if(over!=0)
            {
                pos+=1;
                a[pos]=over;
            }
        }
        printf("%d",a[pos]);//high-five-bit output-length follow its length
        for(i=pos-1; i>=0; i--)
        {
            printf("%05d",a[i]);//others output-length must be five
        }
        printf("\n");
        //for(i=1;i<=n;i++)
        //test*=i;
        //printf("%f\n",test);
    }
    return 0;
}

 

hdu1042 N!

标签:

原文地址:http://www.cnblogs.com/heqinghui/p/4848804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!