nefu 72 N!

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

Hint

输入的N小于10000,可以输入,用INT型的大数就行,用数值型的高精度!


//这道题本身很简单,但是目标是变得更快,于是我采用每次计算N!的位数来减少循环
//同时注意log 是double类型!!!!同时注意log 是double类型!!!!同时注意log 是double类型!!!!


#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
int data[40000];
int main()
{
    int n,k;
    double wei;
    while(cin>>n)
    {
        memset(data,0,sizeof(data));
        data[0]=1;
        wei=log10(1);
        for(int i=2;i<=n;i++)
        {
             int c=0;//每次乘i是的进位
             wei+=log10(i);
             for(int j=0;j<=(int)wei+1;j++)
             {
                 int s=data[j]*i+c;
                 c=s/10;
                 data[j]=s%10;
             }
        }
        //cout<<wei<<endl;
       // for(k=39999;k>=0;k--) if(data[k]!=0) break;
        for(k=wei;k>=0;k--)
        cout<<data[k];
        cout<<endl;
    }
    return 0;
}
 
posted @ 2016-03-22 19:01  邻家那小孩儿  阅读(227)  评论(0编辑  收藏  举报