博士
最后登录1970-1-1
在线时间 小时
注册时间2020-5-14
|
- #include <stdio.h>
- #include <stdlib.h>
- char* real_mult(const char* a, const char* b) {
- int i, j, k, m, n, len_a, len_b;
- len_a = strlen(a);
- len_b = strlen(b);
- int* res_p;
- char* c;
- res_p = (int*)malloc((len_a + len_b) * sizeof(int));
- if (!res_p) {
- printf((const char*)"malloc int[%d] fail!\n", len_a + len_b);
- for ( ; ; );
- }
- for (i = len_a + len_b - 1; i >= 0; i--) {
- res_p[i] = 0;
- }
- for (i = 0; i < len_a; i++) {
- for (j = 0; j < len_b; j++) {
- k = i + j + 1;
- res_p[k] += (a[i] - '0') * (b[j] - '0');
- }
- }
- res_p[0] = 0;
- for ( ; k > 0; k--) {
- res_p[k - 1] += res_p[k] / 10;
- res_p[k] %= 10;
- }
- for (j = 0; j < len_a + len_b; j++) {
- if (res_p[j] != 0) {
- break;
- }
- }
- c = (char*)malloc((len_a + len_b + 1 - j) * sizeof(char));
- if (!c) {
- printf((const char*)"malloc char[%d] fail!\n", len_a + len_b + 1 - j);
- for ( ; ; );
- }
- for (i = 0; i < len_a + len_b - j; i++) {
- c[i] = (char)(res_p[i + j] + '0');
- }
- free(res_p);
- c[i] = '\0';
- return c;
- }
- char* mult1(char* a, const char* b) {
- char* c = real_mult((const char*)a, b);
- free(a);
- return c;
- }
- char* order(int n) {
- char temp[16];
- char* res = (char*)malloc(32);
- for ( ; n > 1; n--) {
- sprintf((char*)temp, "%d", n);
- res = mult1(res, (const char*)temp);
- }
- return res;
- }
- int main(int argc, const char* argv[]) {
- char* c;
- c = order(20000);
- printf("%s\n", c);
- free(c);
- return 0;
- }
复制代码
|
|