N - N!
HDU - 1042
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的阶乘
解法:n最大10000一定超出longlong,高精度计算,数组每一项存4位数,使得与n相乘最大为10^9在int范围内
代码:
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 50000
int x[maxn];
int main()
{
int a;
while(cin>>a){
if(a==0){
cout<<"1"<<endl;
continue;
}
memset(x, 0, sizeof(x));
x[0]=1;
for(int i=2;i<=a;i++){
int temp=0;
for(int j=0;j<maxn;j++){
x[j]=x[j]*i+temp;
temp=x[j]/10000;
x[j]%=10000;
}
}
int flag=0;
for(int i=maxn;i>=0;i--){
if(flag==1)
printf("%04d",x[i]);
else
if(x[i]!=0){
cout<<x[i];
flag=1;
}
}
cout<<endl;
}
}