求救大佬,20分!

P1303 A*B Problem

_Ayanami_Rei_ @ 2022-03-06 10:50:20

#include<iostream>
using namespace std;
char A[510], B[510];
int main(){

    cin >> A >> B;

    if (A[0] == '0' or B[0] == '0') {
        cout << 0;
        return 0;
    }

    int a[510] = {0}, b[510] = {0};
    int lena = strlen(A) - 1, lenb = strlen(B) - 1;
    for (int i = lena;i >= 0; --i) {
        a[i] += A[lena - i] - '0';
    }
    for (int i = lenb;i >= 0; --i) {
        b[i] += B[lenb - i] - '0';
    }

    int c[1020] = {0}, lenc = lenb + lena + 1;
    for (int i = 0;i <= lena; ++i) {
        for (int j = 0;j <= lenb; ++j) {
            c[i + j] = a[i] * b[j];
        }
    }

    for (int i = 0;i <= lenc; ++i) {
        c[i + 1] = c[i + 1] + c[i] / 10;
        c[i] = c[i] % 10;
    }

    while (c[lenc] == 0) {
        --lenc;
    }
    for(int i = lenc;i >= 0; --i) {
        cout<<c[i];
    }
    return 0;
}

by Dream_weavers @ 2022-03-06 10:54:27

python自带高精

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

by ajahjahah @ 2022-03-06 11:25:47

每个数不超过10^2000

开500肯定RE啊


by kbzcz @ 2022-03-06 11:29:00

你这不错才怪


by ajahjahah @ 2022-03-06 11:40:00

@wumengzhe

#include<iostream>
#include<cstring>
using namespace std;
char A[2005], B[2005];//主要看数据范围
int main(){

    cin >> A >> B;

    if (A[0] == '0' or B[0] == '0') {
        cout << 0;
        return 0;
    }

    int a[2005] = {0}, b[2005] = {0};
    int lena = strlen(A) - 1, lenb = strlen(B) - 1;
    for (int i = lena;i >= 0; --i) {
        a[i] += A[lena - i] - '0';
    }
    for (int i = lenb;i >= 0; --i) {
        b[i] += B[lenb - i] - '0';
    }

    int c[4005] = {0}, lenc = lenb + lena + 1;
    for (int i = 0;i <= lena; ++i) {
        for (int j = 0;j <= lenb; ++j) {
            c[i + j] += a[i] * b[j];//+=不然会覆盖之前的
        }
    }

    for (int i = 0;i <= lenc; ++i) {
        c[i + 1] = c[i + 1] + c[i] / 10;
        c[i] = c[i] % 10;
    }
    int flag = 0;
    for(int i = lenc;i >= 0; --i) {//这里要处理前置0
        if(flag == 0){
            if(c[i]!=0){
                cout<<c[i];
                flag=1;
            }
        }
        else{
            cout<<c[i];
        }
    }
    return 0;
}

by ajahjahah @ 2022-03-06 11:40:19

亲测过了


|