hdu1042

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 66145    Accepted Submission(s): 18997


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
 

//這個階乘問題要特別注意判斷需要多大的數組存儲,我調試了好幾次纔好的
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[8001],n;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        memset(a,0,sizeof(a));
        for(i=2,a[0]=1;i<=n;i++)//乘以i
        {
            for(j=0;j<8000;j++) a[j]*=i;
            for(j=0;j<8000;j++)
            {
                a[j+1]+=a[j]/100000;
                a[j]%=100000;
            }
        }
        for(i=8000;i>=0&&!a[i];i--);//忽略前導0
        printf("%d",a[i--]);
        for(;i>=0;i--) printf("%05d",a[i]);
        printf("\n");
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章