一直是WA,但是不知道哪里错了,自己随便测的数据都对呀

P1303 A*B Problem

tttaaattt123 @ 2022-03-28 20:18:48


#include<iostream> 
#include<string.h>
using namespace std;
struct Complex {
    int a[2010];
    int n;
    Complex() {
        memset(a, 0, sizeof(a));
        n = 1;
    }
    Complex(char aa[], int nn) {
        memset(a, 0, sizeof(a));
        n = nn;

        for (int i = nn - 1; i >= 0; i--)
        {
            a[n - 1 - i] = aa[i] - '0';
        }
    }
    friend ostream& operator << (ostream& o, const Complex& b)
    {
        for (int i = b.n - 1; i >= 0; i--)
        {
            o << b.a[i];
        }
        return o;
    }
    Complex operator +(const Complex& b)
    {
        int carry = 0;
        int len = max(n, b.n);
        Complex c;
        c.n = len;
        for (int i = 0; i < len; i++)
        {
            c.a[i] = a[i] + b.a[i] + carry;
            if (c.a[i] >= 10)
            {
                c.a[i] -= 10;
                carry = 1;
            }
            else
            {
                carry = 0;
            }
        }
        if (carry == 1)
        {
            c.a[len] = carry;
            c.n = len + 1;
        }
        return c;
    }
    Complex operator *(const Complex& b)
    {
        int carry = 0;
        //int len = min(n, b.n);
        Complex c;
        //c.n = len;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < b.n; j++)
            {
                c.a[i + j] += a[i] * b.a[j];
                c.a[i + j + 1] += c.a[i + j] / 10;
                c.a[i + j] %= 10;
            }
        }
        c.n = n + b.n;
        if (c.a[c.n] != 0)
        {
            c.n++;
        }
        while (c.a[c.n-1]==0&&c.n>1) c.n--;
        return c;
    }
};
char x1[2010], x2[2010];
int main()
{
    cin >> x1 >> x2;
    int len1 = strlen(x1);
    int len2 = strlen(x2);
    //Complex result = Complex(x1, len1) + Complex(x2, len2);
    Complex result = Complex(x1, len1) * Complex(x2, len2);
    cout << result << endl;
    return 0;
}

by matrix_ok @ 2022-03-28 20:23:29

@tttaaattt123 数组开不够大,应该开a[4010]


by BearBig @ 2022-03-28 20:24:34

@tttaaattt123 额,按常规高精思路做都行啊,贴代码

a=input()
a=int(a)
b=input()
b=int(b)
print(a*b)

by BearBig @ 2022-03-28 20:25:54

@like_AC @tttaaattt123 没错,因为你自测数据太小了, 10^{2000} \times 10^{2000} = 10^{4000},所以说得开大


by tttaaattt123 @ 2022-03-30 11:01:36

@like_AC 谢谢,通过了!!!


by tttaaattt123 @ 2022-03-30 11:02:08

@HeavenGalaxy 懂了!!谢谢你!


|