#include <iostream>
using namespace std;
#define N 1000
int BigNumFactorial(int Num[], int n);
void Print(int Num[], int Index, int n);
int main(void)
{
int n;
int Num [N] = {1, 0}; //将第一位的数字初始化为1, 其余的都初始化为0
cout << "你需要计算哪个数字的阶乘?" << endl;
cin >> n;
int Index = BigNumFactorial(Num, n);
Print(Num, Index, n);
return 0;
}
int BigNumFactorial(int Num[], int n) //大数阶乘
{
int i, j, sum = 1, temp;
int Index = 0; //用于表示数组中的有效位置数
for(i = 2; i <= n; i++)
{
temp = 0;
for(j = 0; j <= Index; j++)
{
sum = i * Num[j] + temp;
if(sum / 10 != 0) //说明sum不止一位数
{
Num[j] = sum % 10;
temp = sum / 10; //所谓的‘进位’
}
else
{
Num[j] = sum;
temp = 0;
}
}
if(temp != 0) //如果‘进位’还没有为0
{
Index = j - 1;
while(temp)
{
Index++;
Num[Index] = temp % 10;
temp /= 10;
}
}
}
return Index;
}
void Print(int Num[], int Index, int n)
{
int i;
cout << n << "阶的结果为: ";
for(i = Index; i >= 0; i--)
{
cout << Num[i];
}
cout << endl;
}