N! HDU 1042

来源:互联网 发布:seo原创怎么写 编辑:程序博客网 时间:2024/06/05 17:46
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

 

 

思路:大数相乘,用数组来存放数字,存放的最大数字为9999,超过了就进位

#include <stdio.h>#define N 10000int main(){    long int a[10000]={0};    long int i,n,k=1,j;    for(i=1;i<N;i++)    {        a[i]=0;    }    a[0]=1;    while(scanf("%d",&n)!=EOF)    {        if(n==1)        {            printf("1");            printf("\n");            continue;        }        for(i=2;i<=n;i++)        {            for(j=0;j<k;j++)            {                a[j]*=i;            }            for(j=0;j<k;j++)            if(a[j]>N)            {                a[j+1]+=a[j]/N;                  a[j]%=N;            }            if(a[k]>0)                k++;        }        printf("%d",a[k-1]);        for(i=k-2;i>=0;i--)        {            printf("%04d",a[i]);        }        k=1;        for(i=1;i<N;i++)        {            a[i]=0;        }        a[0]=1;        printf("\n");    }    return 0;}


 

0 0