80分求助

P1303 A*B Problem

xnbcgshijie @ 2023-07-26 19:52:50

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

char A[MAXN],B[MAXN];
int a[MAXN],b[MAXN],c[MAXN];

int main()
{
    scanf("%s",A + 1);
    scanf("%s",B + 1);
    int la = strlen(A + 1),lb = strlen(B + 1);
    for(int i = 1;i <= la;i++)
        a[i] = A[la - i + 1] - '0';
    for(int i = 1;i <= lb;i++)
        b[i] = B[lb - i + 1] - '0';
    if(la == 1 && a[1] == 0 || lb == 1 && b[1] == 0)
    {
        cout << 0;
        return 0;
    }
    for(int i = 1;i <= la;i++)
        for(int j = 1;j <= lb;j++)
            c[i + j - 1] += a[i] * b[j];
    int lc = 1;
    while(c[lc] > 9 || lc < la + lb - 1)
        c[lc + 1] += c[lc] / 10,c[lc++] %= 10;
    for(int i = lc;i >= 1;i--)
        cout << c[i];
    return 0;
}

最后一个点过不去


by qinshi0308 @ 2023-07-26 19:58:15

c数组要开两倍


by qinshi0308 @ 2023-07-26 19:59:22

因为俩个 10^{2000} 的数相乘积为 10^{4000}


by qinshi0308 @ 2023-07-26 19:59:54

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

char A[MAXN],B[MAXN];
int a[MAXN],b[MAXN],c[2 * MAXN];

int main()
{
    scanf("%s",A + 1);
    scanf("%s",B + 1);
    int la = strlen(A + 1),lb = strlen(B + 1);
    for(int i = 1;i <= la;i++)
        a[i] = A[la - i + 1] - '0';
    for(int i = 1;i <= lb;i++)
        b[i] = B[lb - i + 1] - '0';
    if(la == 1 && a[1] == 0 || lb == 1 && b[1] == 0)
    {
        cout << 0;
        return 0;
    }
    for(int i = 1;i <= la;i++)
        for(int j = 1;j <= lb;j++)
            c[i + j - 1] += a[i] * b[j];
    int lc = 1;
    while(c[lc] > 9 || lc < la + lb - 1)
        c[lc + 1] += c[lc] / 10,c[lc++] %= 10;
    for(int i = lc;i >= 1;i--)
        cout << c[i];
    return 0;
}

by qinshi0308 @ 2023-07-26 20:01:56

@xnbcgshijie


by xnbcgshijie @ 2023-07-26 20:08:34

O2优化之后RE都没有


by qinshi0308 @ 2023-07-26 20:10:39

这个和RE关系不大


|