第一个样例错误

P1303 A*B Problem

zhanxiangkun @ 2023-07-07 15:50:52


#include <bits/stdc++.h>
using namespace std;
int main(){
    string a,b;
    cin>>a>>b;
    int a0[2001],b0[2001],s0[400001];
    for (int i=1;i<=a.size();i++){
        a0[i]=a[i-1]-48;
    }
    for (int i=1;i<=b.size();i++){
        b0[i]=b[i-1]-48;
    }
    if ((a.size()==1&&a0[2000]==0)||(b.size()==1&&b0[2000]==0)){
        cout << 0;
    }
    else
    {
        memset(s0,0,sizeof(s0));
        for (int i=1;i<=a.size();i++){
            for (int j=1;j<=b.size();j++){
                s0[400001-(a.size()+b.size()-(i+j)+1)]+=(a0[i]*b0[j])%10;
                if (a0[i]*b0[j]>=10){
                    s0[400001-(a.size()+b.size()-(i+j)+2)]+=((a0[i]*b0[j])-(a0[i]*b0[j])%10)/10;
                }
            }
        }
        int longlongp;
        for (int i=1;i<400001;i++){
            if (s0[i]!=0){
                longlongp=400001-i;
                break;
            }
        }

        int kptua=1;
        while (kptua<=longlongp){
            if (s0[400001-kptua]>=10){
                if (longlongp<kptua+1){
                    longlongp+=1;
                }
                s0[400001-kptua-1]+=(s0[400001-kptua]-s0[400001-kptua]%10)/10;
                s0[400001-kptua]=s0[400001-kptua]%10;
            }
            kptua+=1;
        }
        for (int ft=400001-longlongp;ft<=400000;ft++){
            cout << s0[ft];
        }   
    }
    return 0   ;
}

by MC00101 @ 2023-07-09 12:38:18

E~~

#include <bits/stdc++.h>
using namespace std;
string s1,s2;
int a[20000],b[20000],c[40000],l,p;
int main(){
cin>>s1>>s2;
for(int i=0;i<s1.size();i++){
a[i]=s1[s1.size()-i-1]-'0';
}
for(int i=0;i<s2.size();i++){
b[i]=s2[s2.size()-i-1]-'0';
}

for(int i=0;i<s1.size();i++){
for(int j=0;j<s2.size();j++)
{
c[i+j]=c[i+j]+a[i]*b[j];
if(c[i+j]>=10)
{
c[i+j+1]=c[i+j+1]+c[i+j]/10;
c[i+j]=c[i+j]%10;
}
}
}

for(int i=s1.size()+s2.size();i>=0;i--){
if(c[i]!=0){
p=i;
break;
}
}
for(int i=p;i>=0;i--)cout<<c[i];
return 0;
}

我看不懂 但是这是AC代码 给个关注可以吗?


by zhanxiangkun @ 2023-07-09 12:50:15

可以的


|