最后一个点过不了怎么办

P1303 A*B Problem

mopeicong @ 2024-08-16 09:57:04

有人看看看一下为什么最后一个点不对

#include<bits/stdc++.h>
using namespace std;
int a[2100],b[2100],c[2100];

int main(){
    string s1,s2;
    cin>>s1>>s2;
    a[0] = s1.size();
    b[0] = s2.size();
    for(int i=s1.size()-1;i>=0;i--){
        a[s1.size()-i] = s1[i]-'0';
    }
    for(int i=s2.size()-1;i>=0;i--){
        b[s2.size()-i] = s2[i]-'0';
    }
    int lena=a[0],lenb=b[0];
    for(int i=1;i<=lena;i++){
        for(int j=1;j<=lenb;j++){
            c[i+j-1] += a[i]*b[j];
        }
    }
    int len=lena+lenb;
    for(int i=1;i<=len;i++){
        c[i+1] += c[i]/10;
        c[i] %= 10;
    }
    if(c[len]==0){
        len--;
    }
    while(c[len]==0 && len>1) len--;
    c[0] = len;
    for(int i=c[0];i>=1;i--){
        cout<<c[i];
    }
    return 0;
}

by lxr_Galaxy @ 2024-08-16 09:59:17

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    char a_str[2005]={};
    char b_str[2005]={};
    cin>>a_str>>b_str;

    int a[2005]={},b[2005]={};
    int len_a=strlen(a_str),len_b=strlen(b_str);
    for(int i=0;i<=len_a-1;i++){
        a[len_a-i-1]=a_str[i]-48;
    }
    for(int i=0;i<=len_b-1;i++){
        b[len_b-i-1]=b_str[i]-48;
    }

    int ans[4010]={};
    int in=0;
    for(int j=0;j<=len_b-1;j++){
        for(int i=0;i<=len_a-1;i++){
            ans[i+j]=a[i]*b[j]+in+ans[i+j];
            in=ans[i+j]/10;
            ans[i+j]%=10;
        }
        ans[len_a+j]=in;
        in=0;
    }
    int len_ans=len_a+len_b;
    while(ans[len_ans-1]==0 && len_ans>1){
        len_ans--;
    }
    for(int i=len_ans-1;i>=0;i--){
        cout<<ans[i];
    }
}

c应该开大点 @mopeicong


by mopeicong @ 2024-08-16 10:01:54

谢谢啊


by Kete @ 2024-08-20 11:25:06

We can easily solve this problem with 28 lines of code.

Do you think this question is difficult?

Then I won't say much and will go straight to the $AC$ code: ```cpp #include<bits/stdc++.h> using namespace std; int a[2010],b[2010],ans[11000001]; string s1,s2; int n,m,x,k; int main() { cin>>s1>>s2; n=s1.length(); for(int i=1; i<=n; i++)a[i]=s1[n-i]-48; m=s2.length(); for(int i=1; i<=m; i++)b[i]=s2[m-i]-48; x=n+m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { ans[i+j-1]+=a[i]*b[j]; if(ans[i+j-1]>=10) { ans[i+j]+=ans[i+j-1]/10; if(i+j-1==x)x++; ans[i+j-1]%=10; } } } for(int i=x; i>=1; i--) { if(k==0&&ans[i]==0)continue; else cout<<ans[i],k++; } if(k==0)cout<<0; } ``` [AC submission record](https://www.luogu.com.cn/record/173741211) ------------ $$The+End$$

|