求助:3个WA,2个RE。

P1303 A*B Problem

He268435456 @ 2024-12-27 20:24:53

明明套了高精乘模版!!!


by MoLing_111 @ 2024-12-27 20:25:33

给个代码啊........


by He268435456 @ 2024-12-28 11:01:22

#include <bits/stdc++.h>
using namespace std;

int main() {
    char a1[100], b1[100];
    int a[100], b[100], c[100], lena, lenb, lenc, i, j, x;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    gets(a1);
    gets(b1);
    lena = strlen(a1);
    lenb = strlen(b1);
    for (i = 0; i <= lena - 1; i++)
        a[lena - i] = a1[i] - 48;
    for (i = 0; i <= lenb - 1; i++)
        b[lenb - i] = b1[i] - 48;
    for (i = 1; i <= lena; i++) {
        x = 0;
        for (j = 1; j <= lenb; j++) {
            c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
            x = c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
        c[i + lenb] = x;
    }
    lenc = lena + lenb;
    while (c[lenc] == 0 && lenc > 1)
        lenc--;
    for (i = lenc; i >= 1; i--)
        cout << c[i];
    cout << endl;
        return 0;
}

@MoLing_111@MoLing_111


by MoLing_111 @ 2024-12-28 23:40:31

@He268435456数组小了,尽量别用char数组,直接用string;数组初始化直接在主函数外就行,memset容易有问题

给个关呗

#include <bits/stdc++.h>
using namespace std;

string a1,b1;
int a[2005], b[2005], c[2000005], lena, lenb, lenc, i, j, x;

int main() {
    getline(cin,a1);
    getline(cin,b1);
    lena = a1.size();
    lenb = b1.size();
    for (i = 0; i <= lena - 1; i++)
        a[lena - i] = a1[i] - 48;
    for (i = 0; i <= lenb - 1; i++)
        b[lenb - i] = b1[i] - 48;
    for (i = 1; i <= lena; i++) {
        x = 0;
        for (j = 1; j <= lenb; j++) {
            c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
            x = c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
        c[i + lenb] = x;
    }
    lenc = lena + lenb;
    while (c[lenc] == 0 && lenc > 1)
        lenc--;
    for (i = lenc; i >= 1; i--)
        cout << c[i];
    cout << endl;
        return 0;
}

by He268435456 @ 2024-12-29 14:06:50

@MoLing_111 thank you!


|