如下:使用数组进行储存数据
#include<iostream>
using namespace std;
void carry(long long fac[]);
void output(long long fac[]);
int main() {
int n;
cin >> n;
long long fac[3000] = {0};
fac[0] = 1;//初始化阶乘数组
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3000; j++) {
fac[j] *= i;
}
carry(fac);//进位
}
output(fac);
return 0;
}
void carry(long long fac[]) {
int i;
for (i = 0; i < 2999; i++) {
if (fac[i] >= 10) {
fac[i + 1] += fac[i] / 10;
fac[i] %= 10;
}
}
}
void output(long long fac[]) {
int le = 0;
for (int i = 2999; i >= 0; i--) {
if (fac[i] != 0&&!le) {
le = 1;
cout << fac[i];
continue;
}
if (le)
cout << fac[i];
}
}