80分求助!麻烦发完整代码

P1303 A*B Problem

Vivian2022 @ 2024-09-22 19:12:42

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int MAXN=5005;
int A[MAXN],B[MAXN],Ans[MAXN],Len_A,Len_B,Len_Ans;
void Read(int *A,int  &Len)
{
    string cur;
    cin>>cur;
    Len=cur.length();
    for(int i=0;i<Len;i++) A[i]=cur[i]-48;
    reverse(A,A+Len);

}
int main()
{   Read(A,Len_A);
    Read(B,Len_B);
    Len_Ans=Len_A+Len_B-1;
    for(int i=0;i<Len_A;i++)
    for(int j=0;j<Len_B;j++)
    Ans[i+j]+=A[i]*B[j];
    for(int i=0;i<Len_Ans;i++)
    if(Ans[i]>9)Ans[i+1]+=Ans[i]/10,Ans[i]%=10;
    while(Ans[Len_Ans])Len_Ans++;

    for(int i=Len_Ans-1;i>=0;i--)
{   

        cout<<Ans[i];

}

  return 0;
}
}

by Malkin_Moonlight @ 2024-09-22 19:18:28

@Vivian2022

用Python3

print(int(input())*int(input()))

by LiZeChen_1 @ 2024-09-22 19:20:11

我写的

你的我实在改不了

#include<iostream>
#include<cstring>
using namespace std;
char a1[50001],b1[50001];
int a[50001],b[50001],i,x,len,j,c[50001];
int main ()
{
    cin >>a1 >>b1;
    a[0]=strlen(a1);b[0]=strlen(b1);
    for (i=1;i<=a[0];++i)a[i]=a1[a[0]-i]-'0';
    for (i=1;i<=b[0];++i)b[i]=b1[b[0]-i]-'0';
    for (i=1;i<=a[0];++i)for (j=1;j<=b[0];++j)c[i+j-1]+=a[i]*b[j];
    len=a[0]+b[0];                                      
    for (i=1;i<len;++i)if (c[i]>9){c[i+1]+=c[i]/10;c[i]%=10;}
    while (c[len]==0&&len>1)len--;
    for (i=len;i>=1;--i)cout <<c[i];
    return 0;
}

by Vivian2022 @ 2024-09-22 19:21:34

@Malkin_Moonlight Σ(⊙▽⊙"a


by Vivian2022 @ 2024-09-22 19:22:01

@LiZeChen_1 谢谢


by shx2011 @ 2024-09-28 13:27:06

@Vivian2022 答案的最高位数应该会比输入位数多的,如果是RE把ans数组开大一点试试


by 47C2023cty @ 2024-10-05 17:29:13

#include<bits/stdc++.h>
using namespace std;
char a1[114514],b1[114514];
int a[114514],b[114514],c[114514];
int main()
{
    cin>>a1;
    cin>>b1;
    int alen=strlen(a1);
    int blen=strlen(b1);
    int clen;
    for(int i=1,j=alen-1;i<=alen;i++,j--)
    {
        a[i]=a1[j]-'0';
    }
    for(int i=1,j=blen-1;i<=blen;i++,j--)
    {
        b[i]=b1[j]-'0';
    }
    for(int i=1;i<=alen;i++)
    {
        int x=0;
        for(int j=1;j<=blen;j++)
        {
            c[i+j-1]=a[i]*b[j]+x+c[i+j-1];
            x=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
        c[i+blen]=x;
    }
    clen=alen+blen;
    while(c[clen]==0&&clen!=1)
    {
        clen--;
    }
    for(int i=clen;i>=1;i--)
    {
        cout<<c[i];
    }
    return 0;
} 

我的c++代码


|