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
题目大意:应该不用多说。。。
解题思路:如果是首次接触这一类问题,就应该看看。
首先保存这n个数都用字符串形式,然后逐位处理,比如
int 结果 = result[x] * 乘数 + 进位;
把每一位的计算结果的个位数拿出来放到这个数组元素上:result[x] = 结果%10;
接下来的工作就是计算出进位:进位 = 结果 / 10;
注意最后一个数应该也要处理即可
最后从后往前输出结果。
#include<iostream>
using namespace std;
const int M=40005;
int main()
{
int n,i,t,k;
int a[100005];
while(cin>>n)
{
memset(a,0,sizeof(a));
a[0]=1;
for(i=2;i<=n;i++)
{
k=0;
for(int j=0;j<M;j++) //核心处理
{
t=a[j]*i+k;
k=t/10;
a[j]=t%10;
}
}
for(i=M-1;;i--) //反转回来的时候注意舍弃前置零
if(a[i])
break;
for(;i>=0;i--)
cout<<a[i];
cout<<endl;
}
return 0;
}