我这代码怎么改??????

P1303 A*B Problem

bobi0577 @ 2023-09-19 17:15:34

#include<bits/stdc++.h>
using namespace std;
struct big{
    int a[2005]={0},len=1;
    void read(){
        string s;
        cin>>s;
        for(int i=0;i<s.size();i++){
            a[s.size()-i]=s[i]-'0';
        }
        len=s.size();
    }
    big operator*(const big x){
        big c;
        for(int i=1;i<=len;i++){
            for(int j=1;j<=x.len;j++){
                c.a[j+i-1]+=x.a[j]*a[i];
                c.a[j+i]+=c.a[j+i-1]/10;
                c.a[j+i-1]%=10;
            }
        }
        for(c.len=1;a[c.len]!=0;c.len++);
        c.len--;
        return c;
    }
    void print(){
        for(int i=len;i>=1;i--)cout<<a[i];
    }
};
int main(){
    big x,y;
    big c;
    x.read();
    y.read();
    c=x*y;
    c.print();
}

求助dalao!!!


by jiangtaizhe001 @ 2023-09-19 18:30:49

a,b\le 10^{2000} ab\le 10^{4000}

by bobi0577 @ 2023-09-19 18:51:18

@jiangtaizhe001 谢谢,不过改了之后还是没用啊


by jiangtaizhe001 @ 2023-09-19 19:22:33

进位要全部乘完之后扫一次


by bobi0577 @ 2023-09-20 21:25:07

@jiangtaizhe001

#include<bits/stdc++.h>
using namespace std;
struct big{
    int a[4005]={0},len=1;
    void read(){
        string s;
        cin>>s;
        for(int i=0;i<s.size();i++){
            a[s.size()-i]=s[i]-'0';
        }
        len=s.size();
    }
    big operator*(big x){
        big c;
        int q[2005][4005]={0};
        for(int i=1;i<=len;i++){
            for(int j=1;j<=x.len;j++){
                q[j][i]=x.a[j]*a[i];
                //c.a[j+i]+=c.a[j+i-1]/10;
                //c.a[j+i-1]%=10;
            }
        }
        for(int i=1;i<=len;i++){
            for(int j=1;j<=x.len;j++){
                c.a[i]+=q[j][i];
            }
            c.a[i+1]+=c.a[i]/10;
            c.a[i]%=10;
        }
        for(c.len=1;a[c.len]!=0;c.len++);
        c.len--;
        return c;
    }
    void print(){
        for(int i=len;i>=1;i--)cout<<a[i];
    }
};
int main(){
    big x,y;
    big c;
    x.read();
    y.read();
    c=x*y;
    c.print();
}

还是不行呀


|