60分求助

P1303 A*B Problem

yang054 @ 2022-09-22 17:57:31

#include<iostream>
using namespace std;
struct hugeint{
    int num[2005]={0};
    int len=0;
};
hugeint strtoint(string s){
    hugeint ans;
    for(int i=s.size()-1;i>=0;i--)
    {
        ans.num[++ans.len]=s[i]-'0';
    }
    return ans;
}
void printint(hugeint n){
    int f=n.len;
    while((n.num[f]==0)&&(f!=1))
    f--;
    for(int i=f;i>=1;i--)
    {
        cout<<n.num[i];
    }
    cout<<endl;
}
hugeint add(hugeint a,hugeint b){
    hugeint ans;

    ans.len=a.len+b.len-1;
    for(int j=1;j<=a.len;j++)
    for(int i=1;i<=b.len;i++)
    {
        ans.num[i+j-1]+=a.num[i]*b.num[j];
        ans.num[i+j]+=ans.num[i+j-1]/10;
        ans.num[i+j-1]=ans.num[i+j-1]%10;
    }
    if(ans.num[ans.len+1]>0)
    ans.len++;
    return ans;
}
int main()
{
    char a[2005],b[2005];
    cin>>a>>b;
    hugeint n1=strtoint(a);
    hugeint n2=strtoint(b);
    printint(add(n1,n2));
    return 0;
}

by yang054 @ 2022-09-22 17:59:04

最后两个不知道怎么错的()


by qinfanxi @ 2022-09-27 18:24:32

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a[100001]={0},b[100001]={0};
    cin>>a>>b;
    int c[100001],d[100001], x=0,ans[100001],i,j;
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    memset(ans,0,sizeof(ans));
    int lena=strlen(a),lenb=strlen(b);
    for(i=0;i<lena;i++) c[i]=(int)(a[lena-i-1]-'0');
    for(i=0;i<lenb;i++) d[i]=(int)(b[lenb-i-1]-'0');
    for(i=0;i<lena;i++)
    {
        x=0;
        for(j=0;j<lenb;j++)
        {
            ans[i+j]=c[i]*d[j]+x+ans[i+j];
            x=ans[i+j]/10;
            ans[i+j]%=10;
        }
        ans[i+j]=x;
    }
    i=i+j-1;
    while(ans[i]==0&&i>0)i--;
    for(int k=i;k>=0;k--) cout<<ans[k];
    return 0;
}

求关注


by qinfanxi @ 2022-09-27 18:25:02

@yang054


by zhizhi_c @ 2022-10-04 16:43:10

@qinfanxi 弱弱地问一句:亲测过了吗?


by qinfanxi @ 2022-10-04 17:40:26

过了


|