分析
用数组来模拟大数阶乘的结果。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 50000;
int n;
int main()
{
cin>>n;
int num[N]={0};
num[0]=1;
int i,j,carry=0; //carry为进位,初始值为0
for(i=2;i<=n;i++)
{
carry=0;
for(j=0;j<N;j++)
{
int temp=i*num[j]+carry;
num[j]=temp%10; //第j位上取mod运算
carry=temp/10; //进位取/10运算
}
}
for(j=0;j<i;j++) //从最低位枚举到第一个非0数为止
{
if(num[j])
{
printf("%d",num[j]);
break;
}
}
return 0;
}