样例错误,求助

P1303 A*B Problem

shangyuang @ 2023-07-16 12:24:26

code

#include<bits/stdc++.h>
using namespace std;
int a[3000],b[3000],i,j,k,c[3000],lena,lenb;
string a1,b1;
int main(){
    cin>>a1>>b1;
    lena=a1.length();
    lenb=b1.length();
    for(i=lena-1;i>=0;i--) {
        a[++j]=a1[i]-'0';
    }
    for(i=lenb-1;i>=0;i--) {
        b[++k]=b1[i]-'0';
    }
    for(i=1;i<=lena;i++){
        for(j=1;j<=lenb;j++){
            c[i+j-1]+=a[i]*b[j];
        }
    }
    for(i=1;i<=2999;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    k=3000;
    while(c[k]==0){
        k--;
    }
    for(i=k;i>=1;i--){
        cout<<c[i];
    }
    return 0;
}

by CNC201101 @ 2023-07-16 13:12:16

不用谢,以前的马蜂好难看

#include<bits/stdc++.h>
using namespace std;
int a[2100],b[2100],c[4200];
string s1,s2;
int main()
{
    cin>>s1>>s2;
    int len1=s1.size(),len2=s2.size();
    for(int i=0;i<len1;i++)
    a[i]=s1[len1-1-i]-'0';
    for(int i=0;i<len2;i++)
    b[i]=s2[len2-1-i]-'0';
    for(int i=0;i<len1;i++)
    for(int j=0;j<len2;j++){
        c[i+j]+=a[i]*b[j];
        c[i+j+1]+=c[i+j]/10;
        c[i+j]%=10;

    }
    int len=len1+len2;
    while(c[len]==0&&len>0) len--;
    for(int i=len;i>=0;i--){
        cout<<c[i];
    }
    return 0;
}

by CNC201101 @ 2023-07-16 13:13:43

@shangyuang


by shangyuang @ 2023-07-16 14:05:53

@CNC201101 能帮我改改吗,两个关注喵


by CNC201101 @ 2023-07-16 14:14:55

@shangyuang 我看看,等一下


by shangyuang @ 2023-07-16 14:16:03

@CNC201101 嗯嗯


by CNC201101 @ 2023-07-16 14:18:37

@shangyuang 有点乱,进位和乘要在一起;前导0那有问题


by shangyuang @ 2023-07-17 17:15:43

@CNC201101 帮我改改呗


|