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;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页