60分求助

P1303 A*B Problem

32duyuchen89 @ 2023-08-29 10:16:41

#include<bits/stdc++.h>
using namespace std;
char s[250];
#define int long long
struct INT{
    int a[250],n;
    INT(){
        memset(a,0,sizeof a);
        n=0;
    }
    void check(){
        while(a[n-1]==0&&n>1) --n;
        if(n==0){
            a[0] = 0;
            n=1;
        }
    }
    void print(){
        for(int i = n-1;i>=0;i--)
        printf("%d",a[i]); 
        putchar('\n');
        cout << endl;
    }
};
INT Read(){
    int i = 0;
    INT A;
    scanf("%s",s);
    A.n = strlen(s);
    for(i = 0;i < A.n;i++)
        A.a[i]=s[A.n-i-1]-'0';
    return A; 
}
INT Mul(INT A,INT B){
    INT C;
    C.n = (A.n+B.n);
    for(int i = 0;i < A.n;i++)
        for(int j = 0;j < B.n;j++){
            C.a[i+j] += A.a[i]*B.a[j];
            if(C.a[i+j] >= 10){
                C.a[i+j+1] += C.a[i+j]/10;
                C.a[i+j]%=10;
            }       
        }
    if(C.a[C.n] >0)
        C.n++;
    C.check();
    return C;
} 
signed main(){
    INT A,B,C;
    A = Read(),B = Read();
    C = Mul(A,B);
    C.print();
    return 0;
}

by kkksbsbsbssb @ 2023-08-29 10:20:07

@32duyuchen89 这样?

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char x[50010],y[50010];
int a[50010],b[50010],c[50010];
int main()
{
    cin>>x>>y;
    a[0]=strlen(x);b[0]=strlen(y);
    for(int i=1;i<=a[0];++i)a[i]=x[a[0]-i]-'0';
    for(int i=1;i<=b[0];++i)b[i]=y[b[0]-i]-'0';
    for(int i=1;i<=a[0];++i)
    for(int j=1;j<=b[0];++j)
    c[i+j-1]+=a[i]*b[j];
    int len=a[0]+b[0];
    for(int i=1;i<=len-1;++i)
    if(c[i]>9)
    {
    c[i+1]+=c[i]/10;c[i]%=10;
    }
    while(c[len]==0&&len>1)len--;
    for(int i=len;i>=1;--i)
    printf("%d",c[i]);
    return 0;
} 

by 32duyuchen89 @ 2023-08-29 10:28:44

谢谢,AC了


|