80分求助,最后一点错了

P1303 A*B Problem

nameless_miao @ 2023-09-02 17:01:14

#include <bits/stdc++.h>
using namespace std;

int a[2005] = {0},b[2005] = {0},c[4005] = {0};
string s1,s2;
bool k = 0;

int main(){
    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 <= s1.size() + s2.size();i++){
        for(int j = 1;j <= i;j++){
            c[i] += a[j] * b[i + 1- j];
            if(c[i] >= 10){
                c[i + 1] += c[i] / 10;
                c[i] %= 10;
            } 
        }
    }
    for(int i = s1.size() + s2.size();i >= 1;i--){
        if(k || c[i] != 0 || i == 1){
            cout<<c[i];
            k = 1;
        }
    }
    return 0;
}

最后一个点过不去


by mlkjy @ 2023-09-10 11:53:52

**这是我的代码,可参考

#include <bits/stdc++.h>
using namespace std;
string a,b;
long long k1[2000],k2[2000],c[4010];
long long a1,a2,m,l;
int main()
{
    cin>>a>>b;
    for (int i=a.size()-1;i>=0;i--) 
    {
        k1[m]=a[i]-'0';
        m++;
    }
    for (int i=b.size()-1;i>=0;i--) 
    {
        k2[l]=b[i]-'0';
        l++;
    }
    for (int i=0;i<=m;i++)
    {
        for (int j=0;j<=l;j++)
        {
            c[i+j]+=k1[i]*k2[j];
        }

    }
    int len=m+l;
    for (int i=0;i<=len;i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int k=4010;
    while(c[len]==0)
    {
        len--;
    }
    for(int i=max(len,0);i>=0;i--)
    {
        cout<<c[i];
    }
}

**


by dongjianhui @ 2023-10-15 16:33:44

你把a数组和b数组定义为4005


|