WA,求调

P1303 A*B Problem

yangcenyou @ 2024-05-19 10:13:26

#include <bits/stdc++.h>
using namespace std;
int main(){
    string a,b;
    char aw[2345],bw[2345];
    int c[11451];
    cin>>a>>b;
    memset(aw,'0',sizeof(aw));
    memset(bw,'0',sizeof(bw));
    memset(c,0,sizeof(c));
    aw[0]=a.size();
    bw[0]=b.size();
    for(int i=1;i<=aw[0];i++) aw[i]=a[aw[0]-i]-'0';
    for(int i=1;i<=bw[0];i++) bw[i]=b[bw[0]-i]-'0';
    for (int i=1;i<=aw[0];i++){
        for (int j=1;j<=bw[0];j++){
            c[i+j-1]+=a[i]*b[j];
        }
    }
    int length=aw[0]+bw[0];
    for(int i=1;i<=length;i++){
        if(c[i]>9){
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
    }
    while(c[length]==0&&length>1) length--;
    for (int i=length;i>=1;--i) cout<<c[i];
    return 0;
}

by OIer_Hhy @ 2024-05-19 11:00:44

@yangcenyou

c[i+j-1]+=a[i]*b[j];

应改为

c[i+j-1]+=aw[i]*bw[j];

My Code(仅供参考):

#include <bits/stdc++.h>
using namespace std;
int main(){
    string a,b;
    int aw[2345]={},bw[2345]={},c[11451]={};
    cin>>a>>b;
    aw[0]=a.size();
    bw[0]=b.size();
    for(int i=1;i<=aw[0];i++) aw[i]=a[aw[0]-i]-'0';
    for(int i=1;i<=bw[0];i++) bw[i]=b[bw[0]-i]-'0';
    for(int i=1;i<=aw[0];i++){
        for(int j=1;j<=bw[0];j++)
            c[i+j-1]+=aw[i]*bw[j];
    }
    int length=aw[0]+bw[0];
    for(int i=1;i<=length;i++){
        if(c[i]>9){
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
    }
    while(c[length]==0&&length>1) length--;
    for (int i=length;i>=1;--i) cout<<c[i];
    return 0;
}

by yangcenyou @ 2024-05-19 11:14:31

谢谢,已AC。

AC记录


|