N!(1042)hdu(大数阶乘)

来源:互联网 发布:linux httpd配置文件 编辑:程序博客网 时间:2024/05/23 11:00

N!

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


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 <stdio.h>
#include <stdlib.h>
#define M 100000
int a[M],i,k,n;
int j,counts;


void init()
{
    for(i=1;i<M;i++)
        a[i] = 0;
}


int main()
{
    init();
      while(scanf("%d",&n)!=EOF)
        {
            a[0]=1; counts = 1;


            for(i=1;i<=n;i++)
            {
                  k = 0;
                   for(j=0;j<counts;j++)
                    {
                                int temp;
                                temp = a[j]*i+k;
                                a[j] = temp%10;
                                k = temp/10;
                    }


                    while(k)
                        
                    {
                        a[counts++] = k%10;
                        k=k/10;
                    }
            }


        // 没有一下2行代码也可以AC
      //      for(j=M-1;j>=0;j--)
     //           if(a[j]==0) break;


              for(i=counts-1;i>=0;i--)
                printf("%d",a[i]);
              printf("\n");
             init();
        }


    return 0;
}


-------------------------------------------------


#include<iostream>
#define MAX 100000
using namespace std;


int main()
{
    int n,a[MAX];
    int i,j,k,count,temp;


    while(cin>>n)
    {
        a[0]=1;
        count=1;


        for(i=1;i<=n;i++)
        {
            k=0;
            for(j=0;j<count;j++)
            {
                temp=a[j]*i+k;
                a[j]=temp%10;
                k=temp/10;
            }


            while(k)//记录进位
             {
                a[count++]=k%10;
                k/=10;
            }
        }


        for(j=MAX-1;j>=0;j--)
            if(a[j])
                break;//忽略前导0


            for(i=count-1;i>=0;i--)
                cout<<a[i];
            cout<<endl;
    }
    return 0;
}



0 0
原创粉丝点击