80分,一个RE,求助

P1303 A*B Problem

ACworld @ 2023-11-12 12:06:01

代码如下

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2005;
char s1[maxn],s2[maxn];
int a[maxn],b[maxn],c[maxn];

int main()
{
    scanf("%s%s",s1,s2);
    int la = strlen(s1);
    int lb = strlen(s2);

    for(int i = 0;i < la;i++)
        a[la - i - 1] = s1[i] - '0';
    for(int i = 0;i < lb;i++)
        b[lb - i - 1] = s2[i] - '0';

    int lc = la + lb;
    memset(c,0,sizeof(c));

    for(int i = 0;i < la;i++)
    {
        for(int j = 0;j < la;j++)
        {
            c[i + j] += a[i] * b[j];
        }
    }
    for(int i = 0;i <= lc;i++)
    {
        if(c[i] >= 10)
        {
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
    }

    while(c[lc] == 0 && lc > 0)
        lc--;

    for(int i = lc;i >= 0;i--)
        printf("%d",c[i]);
        printf("\n");
    return 0;
}

求助!!!


by ZhouYuXuanAllen @ 2023-11-12 12:13:22

C数组大小要开到4005


by DreamCHN @ 2023-11-13 16:34:24

有没有一种可能

Python自带高精度

print(a*b)

要是不会Python也没事,还有JavaBigInteger用上瘾了)

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger a=in.nextBigInteger();
        BigInteger b=in.nextBigInteger();
        BigInteger c=a.multiply(b);
        System.out.println(c);
    }
}

by haolianlian @ 2024-05-19 11:15:26

@ACworld 我写的和你不同

#include<bits/stdc++.h>
using namespace std;
string aa,bb;
int a[10086],b[10086],c[10086];
int main(){
    cin>>aa>>bb;
    int lena=aa.size(),lenb=bb.size(),lenc=lena+lenb;
    for(int i=0;i<lena;i++) a[lena-i-1]=aa[i]-'0';
    for(int i=0;i<lenb;i++) b[lenb-i-1]=bb[i]-'0';
    for(int i=0;i<lena;i++){
        for(int j=0;j<lenb;j++){
            c[j+i]+=a[i]*b[j];
        }
    }
    for(int i=0;i<lenc;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    for(int i=lenc-1;i>=0;i--){
        if(c[lenc-1]==0&&i==lenc-1&&lenc-1>0){
            lenc--;
            continue;
        }
        cout<<c[i];
    }
    return 0;
}

|