Created
December 17, 2023 14:17
-
-
Save Aaron-212/e92aab0c516665478ed911d22dd89a95 to your computer and use it in GitHub Desktop.
超级无敌叼的阶乘计算,使用命令行输入,能算到114514!哦
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define MAX_LENGTH 1048576 | |
int superMultiplication(int a, int *b, int bSize) { | |
int carry = 0; | |
for (int i = 0; i < bSize; i++) { | |
int temp = b[i] * a + carry; | |
b[i] = temp % 10; | |
carry = temp / 10; | |
} | |
while (carry) { | |
b[bSize] = carry % 10; | |
carry /= 10; | |
bSize++; | |
} | |
return bSize; | |
} | |
int superFactorial(int n, int *result, int resultSize) { | |
if (n == 0) { | |
return 1; | |
} | |
while (n > 0) { | |
resultSize = superMultiplication(n, result, resultSize); | |
n--; | |
} | |
return resultSize; | |
} | |
void superPrint(int *result, int resultSize) { | |
for (int i = resultSize - 1; i >= 0; i--) { | |
printf("%d", result[i]); | |
} | |
printf("\n"); | |
} | |
int char2int(char *str) { | |
int result = 0; | |
while (*str) { | |
result = result * 10 + *str - '0'; | |
str++; | |
} | |
return result; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc == 0 || argc > 2) { | |
printf("Error Arguments!"); | |
} | |
int n = char2int(argv[1]); | |
int result[MAX_LENGTH] = {1}; | |
int r = superFactorial(n, result, 1); | |
superPrint(result, r); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment