为什么运算结果会错

P1303 A*B Problem

HbugU @ 2022-05-11 21:07:44

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fr first
#define se second
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
string times(string as,string bs){
    string ans;
    int a[10100]={0},b[10100]={0},c[10100]={0};
    int lena=as.length(),lenb=bs.length();
    for(int i=lena-1,j=1;i>=0;i--,j++){
        a[j]=as[i]-'0';
    }
    for(int i=lenb-1,j=1;i>=0;i--,j++){
        b[j]=bs[i]-'0';
    }
    for(int i=1;i<=lena;i++){
        for(int j=1;j<=lenb;j++){
            c[i+j-1]+=a[i]*b[i];
        }
    }
    int len=lena+lenb;
    for(int i=1;i<=len;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    while(!c[len]) len--;
    for(int i=len;i>=1;i--) ans+=c[i]+'0';
    return ans;
}
int main(){
    FAST;
    string A,B;
    cin>>A>>B;
    cout<<times(A,B);
    return 0;
}

by gaochunzhen @ 2022-05-11 21:22:28

@HbugU 你试试看把第 20 行的 a[i]*b[i] 换成 a[i]*b[j]


by HbugU @ 2022-05-11 21:45:44

@gaochunzhen AC了,谢谢


by Ultra_compass @ 2022-05-12 16:45:16

python 一行解决问题


|