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

 

  

  首先存數:a[0]=4372,a[1]=792,a[2]=1。107924372,從低位到高位每四位存到一個數組元素中。此時,總位數爲3。 

               接着運算:a[0]*15=65580,所以進位爲a[0]/10000=6,a[0]=a[0]%10000=5580。a[1]*15=11880,a[1]=a[1]+6=11886。

                                 進位爲1,a[1]=1886。a[2]*15=15,a[2]=a[2]+1=16,進位爲0。

               輸出:a[2],a[1],a[0]即爲1618865580。要注意的是:如果a[2]=886,那麼該如何輸出?直接輸出:168865580。顯然不對,

                          正確的是16088655880。輸出的原則是:最高位原樣輸出,其它位如果小於1000,則高位補0,一位一補。





 

#include<stdio.h>
int main()
{
    int a[10001];
    int n,p,c,i,j;
    while(scanf("%d",&n)!=EOF)
    {
       p=1;a[1]=1;
       for(i=1;i<=n;i++)
       {
         c=0;
         for(j=1;j<=p;j++)
         {
           a[j]=a[j]*i+c;
           c=a[j]/10000;
           a[j]%=10000;
         }
         if(c>0)
         {
           p++;
           a[p]=c;
         }
       }
       if(a[p])  printf("%d",a[p]);
       for(i=p-1;i>=1;i--)
       printf("%.04d",a[i]);
       printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章