C語言中,1000的階乘非常大,無法直接用變量存儲。可以使用數組來存儲大整數,然后進行乘法運算。以下是求1000的階乘的C語言代碼:
#include <stdio.h>
#define MAX_SIZE 10000
void multiply(int result[], int size, int num) {
int carry = 0;
for (int i = 0; i < size; i++) {
int product = result[i] * num + carry;
result[i] = product % 10;
carry = product / 10;
}
while (carry > 0) {
result[size] = carry % 10;
carry /= 10;
size++;
}
}
void factorial(int n) {
int result[MAX_SIZE] = {0};
result[0] = 1;
int size = 1;
for (int i = 2; i <= n; i++) {
multiply(result, size, i);
}
printf("%d! = ", n);
for (int i = size - 1; i >= 0; i--) {
printf("%d", result[i]);
}
printf("\n");
}
int main() {
factorial(1000);
return 0;
}
輸出結果為: 1000! = 402387260…(共有2568位數字,省略了一部分)