求助啊,五个点,四个过,一个直接re,不懂啊

P1303 A*B Problem

lingyi_ling @ 2024-12-08 13:55:54

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;int as[1005], bs[1005], c[2005];int main  () {  string a, b;
    cin >> a >> b;reverse(a.begin(), a.end());reverse(b.begin(), b.end());int la = a.size();int lb = b.size();
    for (int i = 0; i < la; i++) {
        as[i] = a[i] - 48;
    }
    for (int i = 0; i < lb; i++) {
        bs[i] = b[i] - 48;
    }
    for (int i = 0; i <= la; i++) {
        for (int j = 0; j <= lb; j++) {
            c[i + j] = c[i + j] + as[i] * bs[j];
        }
    }
    for (int i = 0; i <= 2000; i++) {
        if (c[i] > 9) {
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
    }
    int i = 2000;
    while (c[i] == 0 && i != 0) {
        i--;
    }
    for (int j = i; j >= 0; j--) {
        cout << c[j];
    }
    return 0;
}

by banta_banta @ 2024-12-08 14:09:25

测试数据两数小于 10^{2000},你这个数组只有 1005,肯定会炸。

所有的数组大小改成 4005(或 a,b 2005,c 4005)即可。


by banta_banta @ 2024-12-08 14:10:10

@lingyi_ling


by LionBlaze @ 2024-12-08 14:10:22

@lingyi_ling 显然,我们有没个整数不超过 10^{2000}


by LionBlaze @ 2024-12-08 14:10:53

*每个


by lingyi_ling @ 2024-12-08 14:12:32

thank@LionBlaze@banta_banta


|