0分求调,本地跑过了(AC必关)

P1303 A*B Problem

maobai @ 2024-10-11 15:55:52


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 自动获取字符串的函数
char* autoget() {
    char* string = (char*)calloc(1, sizeof(char));
    char c;
    int i = 0;
    while ((c = getchar()) != '\n' ) {
        string = (char*)realloc(string, sizeof(char) * (i + 2));
        string[i++] = c;
    }
    string[i] = '\0';
    return string;
}

// 主函数
int main() {
    char* number1 = autoget(); // 获取数字 number1
    char* number2 = autoget(); // 获取数字 number2

    int n = strlen(number1); // 获取 number1 的长度
    int m = strlen(number2); // 获取 number2 的长度
    int arr1[2001] = { 0 }; // 初始化
    int arr2[2001] = { 0 }; // 初始化
    int sum_number[4005] = { 0 }; // 初始化

    // 将字符串转化为数组的形式
    for (int i = 0; i < n; i++) {
        arr1[n - 1 - i] = number1[i] - '0'; // 倒序存储
    }
    for (int i = 0; i < m; i++) {
        arr2[m - 1 - i] = number2[i] - '0'; // 倒序存储
    }

    // 进行乘法运算
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            sum_number[i + j] += arr1[i] * arr2[j];
        }
    }

    // 进位处理
    for (int i = 0; i < 4005; i++) {
        if (sum_number[i] >= 10) {
            sum_number[i + 1] += sum_number[i] / 10; // 进位
            sum_number[i] %= 10; // 当前位
        }
    }

    // 输出结果
    int start = 4004; // 从高位开始
    while (start > 0 && sum_number[start] == 0) {
        start--; // 跳过前导零
    }

    if (start < 0) {
        printf("0\n"); // 若结果为零
    }
    else {
        for (; start >= 0; start--) {
            printf("%d", sum_number[start]);
        }
    }

    // 释放内存
    free(number1);
    free(number2);

    return 0;
}

by 1179083629dsm @ 2024-10-27 21:33:08

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
char* autoget() {
    char* string = (char*)calloc(1, sizeof(char));
    char c;
    int i = 0;
    while ((c = getchar()) != '\n' ) {
        string = (char*)realloc(string, sizeof(char) * (i + 2));
        string[i++] = c;
    }
    string[i] = '\0';
    return string;
}
*/
int main() {
    char number1[2001];
    char number2[2001];
    scanf("%s",&number1);
    getchar();
    scanf("%s",&number2);

    int n = strlen(number1); 
    int m = strlen(number2);
    int arr1[2001] = { 0 };
    int arr2[2001] = { 0 };
    int sum_number[4005] = { 0 };
    for (int i = 0; i < n; i++) {
        arr1[n - 1 - i] = number1[i] - '0';
    }
    for (int i = 0; i < m; i++) {
        arr2[m - 1 - i] = number2[i] - '0';
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            sum_number[i + j] += arr1[i] * arr2[j];
        }
    }

    for (int i = 0; i < 4005; i++) {
        if (sum_number[i] >= 10) {
            sum_number[i + 1] += sum_number[i] / 10;
            sum_number[i] %= 10; 
        }
    }

    int start = 4004;
    while (start > 0 && sum_number[start] == 0) {
        start--;
    }

    if (start < 0) {
        printf("0\n"); 
    }
    else {
        for (; start >= 0; start--) {
            printf("%d", sum_number[start]);
        }
    }

    return 0;
}

这不是过了吗?(学什么不好非要学搞内存)


|