【大数】N!

描述

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

输入

One N in one line, process to the end of file.

输出

For each N, output N! in one line.

样例输入

1
2
3

样例输出
1
2
6
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[50050],n;
while(cin>>n)
{
int k;
memset(a,0,sizeof(a));
a[0]=1;
int t=1,c;
for (int i=1;i<=n;i++)
{
k=0;
for (int j=0;j<t;j++)
{
c=a[j]*i+k;
a[j]=c%10;
k=c/10;
}
while(k)
{
a[t++]=k%10;
k=k/10;
}
}
for (int i=t-1;i>=0;i–) cout<<a[i];
cout<<endl;
}
return 0;
}

分析:大数模板题,cin和cout比scanf快,或者preread也行。

发布了40 篇原创文章 · 获赞 0 · 访问量 694

猜你喜欢

转载自blog.csdn.net/Skynamer/article/details/103105590