主要参考了
http://stackoverflow.com/questions/11446973/find-factorial-of-large-numbers-in-java
#include
// 数组乘法, res_size: 表示有多少位, 返回结果的位数
int multiply(int x, int res[], int res_size) {
int carry = 0; // 进位
for (int i=0; iint prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod/10;
}
while (carry!=0) {
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
void factorial(int n) {
int res[36000]; // 10000! 位数不超过36000
// 初始化
res[0] = 1;
int res_size = 1; // 表示有多少位
// 计算 n!
for (int x=2; x<=n; x++) {
res_size = multiply(x, res, res_size);
}
printf("%d != \n", n);
for (int i=res_size-1; i>=0; i--) {
printf("%d", res[i]);
}
printf("\n");
}
int main() {
factorial(1000);
}