hdu1042

来源:互联网 发布:spss软件的应用 编辑:程序博客网 时间:2024/05/17 22:10

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
123
 

Sample Output
126
 

//这个阶乘问题要特别注意判断需要多大的数组存储,我调试了好几次才好的#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;}

0 0