#5 WA 求助!!!

P1303 A*B Problem

wuxiyi @ 2022-11-29 19:28:14

#include<bits/stdc++.h>
using namespace std;
int a[2009]={0},b[2009]={0},c[9010]={0};
int main()
{
    string s1,s2;
    cin>>s1;
    for (int i=s1.size()-1;i>=0;i--)
    {
        a[s1.size()-i]=s1[i]-'0';
    }
    cin>>s2;
    for (int i=s2.size()-1;i>=0;i--)
    {
        b[s2.size()-i]=s2[i]-'0';
    }
    for (int i=1;i<=2004;i++)
    {
        for (int j=1;j<=2004;j++)
        {
            c[i+j-1]+=(a[i]*b[j]);
        }
    }
    for (int i=1;i<=2004;i++)
    {
        if (c[i]>=10)
        {
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
    }
    int i=6003;
    bool out=false;
    while (c[i]==0)
    {
        i--;
    }
    for (int j=i;j>0;j--)
    {
        out=true;
        cout<<c[j];
    }
    if (!out)
    {
        cout<<0;
    }
    return 0;
}

by Felix_Linda @ 2022-11-29 20:59:49

AC代码,仅供参考!!!

切勿复制粘贴!!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3000000;

struct num{
    int a[N],len;
};

void read(num &x){
    string s;
    cin>>s;
    x.len=s.size();
    memset(x.a,0,sizeof(x.a));
    for(int i=0;i<s.size();i++){
        x.a[x.len-i]=s[i]-'0';
    }
}

void print(num x){
    for(int i=x.len;i;i--){
        putchar(x.a[i]+'0');
    }
}

num operator * (num x,num y){
    num ret;
    ret.len=x.len+y.len;
    memset(ret.a,0,sizeof(ret.a));
    for(int i=1;i<=x.len;i++){
        for(int j=1;j<=y.len;j++){
            ret.a[i+j-1]+=x.a[i]*y.a[j];
        }
    }
    for(int i=1;i<=ret.len;i++){
        ret.a[i+1]+=ret.a[i]/10;
        ret.a[i]%=10;
    }
    while(ret.len&&ret.a[ret.len]==0){
        ret.len--;
    }
    if(!ret.len){
        ret.len++;
    }
    return ret;
}

num x,y,ans;

int main(){
    read(x);
    read(y);
    ans=x*y;
    print(ans);
}

by ybc20250528yaojinyi @ 2022-12-11 14:12:21

a,b=map(int,input().split()) print('%d'%(a*b))


|