输入N求N的阶乘的准确值。
Input
输入N(1 <= N <= 10000)
Output
输出N的阶乘
Input示例
5
Output示例
120
思路:开一个数组 每一位数组内存够3位来减少时间(存一位的普通方法会有一组超时) 但是这么做有个坑就是可能这一位要存
的数字是040 但是却只能存进去40 ,具体解决方法在代码中阐述
#include<bits/stdc++.h>
using namespace std;
long long N,A[1000005];
int main()
{
ios::sync_with_stdio(false);
cin>>N;
A[0]=1;
long long D=1;
for(int i=2;i<=N;i++)
{
long long num=0;
for(int j=0;j<D;j++)
{
num=A[j]*i+num;
A[j]=num%1000;
num=num/1000;
}
while(num)
{
A[D]=num%1000;
D++;
num=num/1000;
}
}
for(int i=D-1;i>=0;i--)
{
if(A[i]/100==0&&i!=D-1) //针对缺少0的情况,我们检查这个数字的位数,如果是两位就输出一个0,如果是一位就输出两个0
{
if(A[i]/10==0) cout<<"00";
else cout<<"0";
}
cout<<A[i];
}
cout<<endl;
return 0;
}