全RE求助(关于运算符重载)

P1303 A*B Problem

mortal05 @ 2021-01-12 17:51:42

本地跑没有问题,一提交全RE了

#include <bits/stdc++.h>
using namespace std;
struct BigInt{
    static const int BASE=1e8;
    static const int WIDTH=8;
    vector <int> s;

    BigInt (long long num=0) {*this=num;}
    BigInt operator = (long long num){
        s.clear();
        do{
            s.push_back(num%BASE);
            num/=BASE;
        } while(num>0);
        return *this;
    }
    BigInt operator = (const string& str){
        s.clear();
        int x,len=(str.length()-1)/WIDTH+1;
        for(int i=0;i<len;i++){
            int end=str.length()-i*WIDTH;
            int start=max(0,end-WIDTH);
            sscanf(str.substr(start,end-start).c_str(),"%d",&x);
            s.push_back(x);
        }
        return *this;
    }
    BigInt operator + (const BigInt& b) const {
        BigInt c;
        c.s.clear();
        for(int i=0,g=0;;i++)
        {
            if(g==0&&i>=s.size()&&i>=b.s.size()) break;
            int x=g;
            if(i<s.size()) x+=s[i];
            if(i<b.s.size()) x+=b.s[i];
            c.s.push_back(x%BASE);
            g=x/BASE;
        }
        return c;
    }
    BigInt operator += (const BigInt& b) {
        *this=*this+b; return *this;
    }
    BigInt operator * (const BigInt& b) const {
        BigInt c; c.s.clear(); long long n;
        for(int i=0;i<b.s.size()+s.size();i++) c.s.push_back(0);
        for(int i=0;i<b.s.size();i++)
            for(int j=0;j<s.size();j++) {
                n=((long long)b.s[i])*((long long)s[j]);
                c.s[i+j+1]+=(c.s[i+j]+(int)(n%BASE))/BASE;
                c.s[i+j]=(c.s[i+j]+(int)(n%BASE))%BASE;
                c.s[i+j+2]+=(c.s[i+j+1]+(int)(n/BASE))/BASE;
                c.s[i+j+1]=(c.s[i+j+1]+(int)(n/BASE))%BASE;
            }
        for(int i=c.s.size()-1;i>0;i--) if(c.s[i]==0) c.s.pop_back();
        return c;           
    }
    BigInt print(){
        for(int i=s.size()-1;i>=0;i--) printf("%d",s[i]);
    }

};
int main()
{
    string s1,s2;
    cin>>s1>>s2;
    BigInt a,b,c;
    a=s1;
    b=s2;
    c=a*b;

    c.print();

}

by wsyhb @ 2021-01-12 20:16:50

@mortal05 将 print() 函数返回值的类型从 BigInt 改成 void 即可不 RE


by mortal05 @ 2021-01-13 09:35:54

@wsyhb
谢谢大佬


by Link_Cut_Y @ 2021-07-18 10:26:00

你用vector这样就可以了

#include<iostream>
#include<vector>
using namespace std;
vector<int> mul(vector<int> &A,vector<int> &B)
{
    int t=0;
    vector<int> C(A.size()+B.size(),0);
    for(int i=0;i<A.size();i++)
    {
        for(int j=0;j<B.size();j++)
        {
            t+=A[i]*B[j];
            C[i+j]+=A[i]*B[j];
        }
    }
    for(int i=0;i<C.size()-1;i++)
    {
        C[i+1]+=C[i]/10;
        C[i]%=10;
    }
    while(C.size()>1&&C.back()==0) C.pop_back();
    return C;
}
int main()
{
    string a;
    string b;
    cin>>a>>b;
    vector<int> A,B;
    for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');
    for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');

    vector<int> C=mul(A,B);
    for(int i=C.size()-1;i>=0;i--) cout<<C[i];
    return 0;
}

|