hdu1042


N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25136    Accepted Submission(s): 6967


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

Statistic |  Submit |  Discuss |  Note

这个题目就是用数组的一个空间来储存四个数字。

#include <stdio.h>

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        int a[9000]={1};
        int i,len=1,j;
        for(i=2;i<=n;i++)
        {
            for(j=0;j<len;j++)
            {
                a[j]*=i;
            }


            for(j=0;j<len-1;j++)
            {
                a[j+1]+=a[j]/10000;
                a[j]%=10000;
            }


            if(a[len-1]>10000)
            {
                a[len]=a[len-1]/10000;
                a[len-1]%=10000;
                len++;
            }
        }
        printf("%d",a[len-1]);
        for(i=len-2;i>=0;i--)
        {
            printf("%04d",a[i]);
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(hdu1042)