60分崩溃了,求助大佬

P1303 A*B Problem

s3149594002 @ 2023-11-23 17:00:44

#define _CRT_SECURE_NO_WARNINGS 1

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

#define N 50010

int main()
{
    string A, B;
    int a[N], b[N], c[N] ;
    memset(c, 0, sizeof c);
    cin >> A >> B;
    int lena = A.length(), lenb = B.length();

    for (int i = lena - 1; i >= 0; i--)
        a[lena-i] = A[i] - '0';
    for (int i = lenb - 1; i >= 0; i--)
        b[lena-i] = B[i] - '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]在“计算贡献”时已经是各贡献值的和,因此比高精度和少了一步
        c[i + 1] += c[i] / 10;
        c[i] %= 10;

    }
    while (!c[len])
        len--;
    for (int i = max(1,len); i >=1; i--)   //用max是因为可能有 0 的情况
        cout << c[i];
}

by return0114514 @ 2023-11-23 17:18:03

#include<bits/stdc++.h>
using namespace std;
char a1[10001],b1[10001];
int a[10001],b[10001],i,x,len,j,c[10001];
int main ()
{
    cin>>a1>>b1;
    int lena=strlen(a1);
    int lenb=strlen(b1);
    for(i=1;i<=lena;i++)
    {
        a[i]=a1[lena-i]-'0';
    }
    for(i=1;i<=lenb;i++)
    {
        b[i]=b1[lenb-i]-'0';
    }
    for(i=1;i<=lenb;i++)
    for(j=1;j<=lena;j++)
    c[i+j-1]+=a[j]*b[i];
    for(i=1;i<lena+lenb;i++)
    if(c[i]>9)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    len=lena+lenb;
    while(c[len]==0&&len>1)
    {
        len--;
    }
    for(i=len;i>=1;i--)
    {
        cout<<c[i];
    }
    return 0;
}

|