40 求助大佬 实在看不出哪儿错了

P1303 A*B Problem

czy189 @ 2024-07-30 17:30:15

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

vector<int> mul(vector<int> A, vector<int> B)
{
    long long t = 0;
    vector<int> C;
    for(int i = 0; i < A.size() || t;++i)
    {
        if(i < A.size())
        {
        for(int j = 0; j < B.size(); ++j)
        {

            t += A[i] * B[j] * pow(10,j);
        }
        }
        C.push_back(t % 10);
        t /= 10;
    }
    while(C.back() == 0 && C.size() > 1) C.pop_back();
    return C;
}
int main()
{
    string a, 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');

    auto C = mul(A,B);
    for(int i = C.size()-1;i >= 0; --i) printf("%d", int(C[i]));
    return 0;
}

by haimingbei @ 2024-07-30 17:40:44

@czy189

(AC,求关注)

其实,你可以这样做

/*
3、P1303 A*B Problem 
*/
#include<bits/stdc++.h>
using namespace std;
char s1[2005],s2[2005];
int a1[2005],a2[2005],a3[4010];
int main(){
    cin>>s1>>s2;
    int lena=strlen(s1),lenb=strlen(s2),len=lena+lenb;
    for(int i=1;i<=lena;i++)a1[i]=s1[lena-i]-'0';
    for(int i=1;i<=lenb;i++)a2[i]=s2[lenb-i]-'0';
    if(a1[1]==0||a2[1]==0){
        cout<<0;
        return 0;
    }
    for(int i=1;i<=lena;i++){
        for(int j=1;j<=lenb;j++){
            a3[i+j-1]+=a1[i]*a2[j];
        }
    }
    for(int i=1;i<=len;i++){
        a3[i+1]+=a3[i]/10;
        a3[i]%=10;
    }
    while(a3[len]==0)len--;
    for(int i=len;i>=1;i--)cout<<a3[i];
    return 0;
}

|