hdu 1042 N!(萬進制)

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(極光炫影)

代碼:

#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
int i,j,k,r,n,a[10004];
int main()
{
    while(cin>>n)
    {
        memset(a,-1,sizeof(a));
        k=0,r=1,a[0]=1;
        for(i=1;i<=n;i++)
        {
            k=0;
            for(j=0;j<r;j++)
            {
                a[j]=a[j]*i+k;
                k=a[j]/10000;
                a[j]%=10000;
            }
            if(k>0)
                a[r++]=k;
        }
        cout<<a[r-1];
        for(i=r-2;i>=0;i--)
            cout<<setw(4)<<setfill('0')<<a[i];
        cout<<endl;
    }
    return 0;
}

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