N!高精度

来源:互联网 发布:下周主要财经数据 编辑:程序博客网 时间:2024/04/30 07:14

计算N!

Problem:A

Time Limit:6000ms

Memory Limit:65536K

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

Hint

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

Source



#include <iostream>#include <string.h>using namespace std;///求N的阶const int maxn=50005;int f[maxn];int main(){    int n,c;    while(cin>>n)    {        memset(f,0,sizeof(f));        f[0]=1;        int i;        for( i=1;i<=n;i++)        {            c=0;            for(int j=0;j<=maxn;j++)            {                int s=f[j]*i+c;                f[j]=s%10;                c=s/10;            }        }        for( i=maxn;i>=0;i--)        {            if(f[i]!=0)            {               break;            }        }        for(;i>=0;i--)        {            cout<<f[i];        }        cout<<endl;    }    //cout << "Hello world!" << endl;    return 0;}


原创粉丝点击