求助大佬,第一个测试点过不了

P1303 A*B Problem

刘先生 @ 2019-08-11 16:21:00

#include<stdio.h>
#include<string.h>
char ch1[5000] = {0};
char ch2[5000] = {0};
int num1[5000] = {0};
int num2[5000] = {0};
int ans[5000] = {0};
int main() {
    scanf("%s", ch1);
    scanf("%s", ch2);
    int len1 = strlen(ch1);
    int len2 = strlen(ch2);
    for(int i = 0; i < len1; i++) {
        num1[len1 - i] = ch1[i] - '0';
    }
    for(int j = 0; j < len2; j++) {
        num2[len2 - j] = ch2[j] - '0';
    }
    for(int i = 1; i <= len1; i++) {
        for(int j = 1; j <= len2; j++) {
            ans[j + i] +=  num1[i] * num2[j];
        }
    }
    ans[0] = len1 + len2;
    for(int i = 2; i <= ans[0]; i++) {
        if(ans[i] < 10) continue;
        ans[i + 1] += ans[i] / 10;
        ans[i] %=  10;
        ans[0] += (i == ans[0]);
    }
    while (ans[ans[0]] == 0 && ans[0] > 1){
        ans[0]--;
    }
    for(int i = ans[0]; i >= 2; i--) {
        printf("%d", ans[i]);
    }
}

by 刘先生 @ 2019-08-11 16:34:23

已解决

#include<stdio.h>
#include<string.h>
char ch1[5000] = {0};
char ch2[5000] = {0};
int num1[5000] = {0};
int num2[5000] = {0};
int ans[5000] = {0};
int main() {
    scanf("%s", ch1);
    scanf("%s", ch2);
    int len1 = strlen(ch1);
    int len2 = strlen(ch2);
    for(int i = 0; i < len1; i++) {
        num1[len1 - i] = ch1[i] - '0';
    }
    for(int j = 0; j < len2; j++) {
        num2[len2 - j] = ch2[j] - '0';
    }
    for(int i = 1; i <= len1; i++) {
        for(int j = 1; j <= len2; j++) {
            ans[j + i - 1] +=  num1[i] * num2[j];
        }
    }
    ans[0] = len1 + len2;
    for(int i = 1; i <= ans[0]; i++) {
        if(ans[i] < 10) continue;
        ans[i + 1] += ans[i] / 10;
        ans[i] %=  10;
        ans[0] += (i == ans[0]);
    }
    while (ans[ans[0]] == 0 && ans[0] > 1){
        ans[0]--;
    }
    for(int i = ans[0]; i >= 1; i--) {
        printf("%d", ans[i]);
    }
}

by Skyjoy @ 2019-08-11 16:43:45

python大法好


by XyzL @ 2019-08-11 16:44:49

python大法好


by 闫立 @ 2019-08-11 16:51:42

python大法好

python大法好

python大法好

python大法好

python大法好
python大法好

|