最后一个点wa

P1303 A*B Problem

Play_CP_4fun @ 2022-04-08 12:32:42

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;
int a[maxn] = {0}, b[maxn] = {0}, c[maxn * 2] = {0};
int main() {
    string s1, s2;
    cin >> s1 >> s2;
    int l1 = s1.length(), l2 = s2.length();
    for (int i = 0; i < l1; i++) a[i] = s1[l1 - 1 - i] - '0';
    for (int i = 0; i < l2; i++) b[i] = s2[l2 - 1 - i] - '0';

    for (int i = 0; i < l1 + l2 - 1; i++) {
        for (int j = 0; j <= i; j++) {
            c[i] += a[j] * b[i - j];
        }

        if (c[i] >= 10) {
            c[i + 1] += c[i] / 10;
            c[i] = c[i] % 10;
        }
    }

    int mark = maxn * 2;
    for (; mark >= 1; mark--) {
        if (c[mark] != 0) break;
    }
    for (; mark >= 0; mark--) {
        cout << c[mark];
    }
    return 0;
}

实在不知道哪里错了。。。


by yyandy @ 2022-04-08 12:46:03

@Play_CP_4fun maxn * 2 越界了


by panyanppyy @ 2022-04-08 12:48:49

2000*2


by Play_CP_4fun @ 2022-04-08 12:49:24

@yyandy 好吧我看题目数据范围,是2000位的,开4020位还会越界吗


by yyandy @ 2022-04-08 12:53:38

@Play_CP_4fun 为啥我开到3000就 A 了,我先看看。


by yyandy @ 2022-04-08 12:54:34

@Play_CP_4fun 你计算乘法的时候 a 数组越界了。


by yyandy @ 2022-04-08 12:55:11

for (int j = 0; j <= i; j++) {
       c[i] += a[j] * b[i - j];
 }

这段 i 最大可以到达 4000


by Play_CP_4fun @ 2022-04-08 12:55:49

@yyandy 晕,就是说a,b也要开到maxn*2对吗


by Play_CP_4fun @ 2022-04-08 12:56:43

@yyandy 谢谢!!!


|