20分蒟蒻求助

P1303 A*B Problem

QBZ951_wc @ 2024-08-01 14:56:35

前导0删的有问题,0全删了,不会改了。

#include<iostream>
using namespace std;

int a1[22222],a2[22222],ans[44444];

int main(){
    string s1,s2;
    cin>>s1>>s2;
    if(s1=="0"||s2=="0"){
        cout<<0;
        return 0;
    }
    int ls1=s1.length(),ls2=s2.length();
    int mls=ls1+ls2;
    for(int i=0;i<ls1;i++)
        a1[i]=s1[i]-'0';
    for(int i=0;i<ls2;i++) 
        a2[i]=s2[i]-'0';
    for(int i=0;i<ls1;i++)
        for(int j=0;j<ls2;j++)
            ans[i+j]=a1[i]*a2[j];
    while(ans[mls]==0)
        mls--;
    for(int i=mls;i>=0;i--){
        if(ans[i]>9){
            ans[i+1]+=ans[i]/10;
            ans[i]/=10;
        }
    }
    if(ans[mls+1]!=0) cout<<ans[mls+1];
    for(int i=0;i<=mls;i++) cout<<ans[i];
    return 0;
}

by haimingbei @ 2024-08-01 15:02:29

@QBZ951_wc

(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;
}

by QBZ951_wc @ 2024-08-02 08:44:08

@haimingbei 感谢


|