萌新40分求助QAQ,第一个点没错但不知为啥过不了导致后面案例看不到

P1303 A*B Problem

Mayueriu @ 2023-03-11 20:27:08

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
using namespace std;

string plus_(string a, string b) {
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    int arr[1000] = { 0 };
    int s1[1000] = { 0 }, s2[1000] = { 0 };
    for (int i = 0; i < a.size(); i++) s1[i] = a[i] - '0';
    for (int i = 0; i < b.size(); i++) s2[i] = b[i] - '0';
    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < b.size(); j++) {
            arr[i + j] += s1[i] * s2[j];
        }
    }
    string c; int fl = 0;
    for (int i = 0; i <= a.size()*b.size(); i++) {
        arr[i + 1] += arr[i] / 10;
        arr[i] %= 10;
        c.insert(c.begin(), arr[i] + '0');
    }
    while (c[0] == '0')
        c.erase(0, 1);
    if (c == "") c = '0';
    return c;

}

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    cout << plus_(s1, s2);

}

by Liyucong1234 @ 2023-03-11 21:02:04


#include<bits/stdc++.h>//万能头
using namespace std;
int a[10100],b[10100],c[10100];
string s,ss;
int main()
{
    cin>>s>>ss;
    int lena=s.length();
    int lenb=ss.length();
    for(int i=0;i<lena;i++)
    a[lena-i-1]=s[i]-'0';
    for(int i=0;i<lenb;i++)
    b[lenb-i-1]=ss[i]-'0';
    int lenc=lena+lenb;
    for(int i=0;i<lena;i++)
    {
        int x=0;
        for(int j=0;j<lenb;j++)
        {
            c[i+j]+=a[i]*b[j]+x;
            x=c[i+j]/10;
            c[i+j]%=10;
        }
        c[i+lenb]=c[i+lenb]+x;
    }
    while(lenc>1&&c[lenc-1]==0)
    lenc--;
    for(int i=lenc-1;i>=0;i--)
    cout<<c[i];
    return 0;

|