求大神Help!!!

P1303 A*B Problem

呼啸山庄 @ 2017-08-02 20:32:16

#include<cstdio>
#include<cstring>
#include<iostream>
int A[50000],B[50000],Ans[100000],ALen,BLen,Len;
char Ac[50000],Bc[50000];
void Print(){
        for(int i = Len-1;i >= 0;i--) printf("%d",Ans[i]);
}
void Multiply(){
        for(int i = 0;i < ALen;i++){
                for(int j = 0;j < BLen;j++) {
                        Ans[i+j] += (A[j]*B[i]);
                        if(Ans[i+j] >= 10){
                                Ans[i+j+1] += (Ans[i+j]/10);
                                Ans[i+j] %= 10;
                                if(i+j+1 >= Len) Len++;
                        }
                }    
        }
        for(int i = Len-1;i > 0 && Ans[i] == 0;i--) Len--;
}
void Prepare(){
        for(int i = 0;i < ALen;i++) A[ALen-i-1] = Ac[i]-'0';
        for(int i = 0;i < BLen;i++) B[BLen-i-1] = Bc[i]-'0';
        Len = std::max(ALen,BLen);
}
int main(){
        char tmp;
        scanf("%s",Ac);
        scanf("%s",Bc);
        ALen = strlen(Ac);
        BLen = strlen(Bc);
        Prepare();
        Multiply();
        Print();
        return 0;
}

by DanStevens @ 2017-08-07 22:33:30

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=1000005;
bool bb;
char a1[mm],b1[mm],c1[mm];
int a[mm],b[mm],c[mm],la,lb,lc=1,i,x;
int main()
{
    scanf("%s",a1);
    scanf("%s",b1);
    bb=strlen(a1)<strlen(b1);
    bb=bb||(strlen(a1)==strlen(b1))&&(strcmp(a1,b1)<0);
       if (bb)
    {
        strcpy(c1,a1);
        strcpy(a1,b1);
        strcpy(b1,c1);
        cout<<'-';
    }
    la=strlen(a1);
    lb=strlen(b1);
    for (i=0; i<=la-1; i++) a[la-i]=a1[i]-'0';
    for (i=0; i<=lb-1; i++) b[lb-i]=b1[i]-'0';
    while (lc<=la||lc<=lb)
    {
        if (a[lc]<b[lc])
        {
            a[lc]+=10;
            a[lc+1]--;
        }
        c[lc]=a[lc]-b[lc];
        lc++;
    }
    while (c[lc]==0&&lc>=2) lc--;
    for (i=lc; i>=1; i--) cout<<c[i];
    return 0;
}
///本人不是大神,但想出此做法,仅供参考。

by 香风智乃 @ 2017-08-10 18:18:29

@DanStevens 您的怕不是 a-b 不过这题貌似是a*b


by DanStevens @ 2017-08-13 19:30:10

不好意思,发错了。。。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=5005;
char a1[mm],b1[mm];
int a[mm],b[mm],c[mm],la,lb,lc=1,i,j,x;
int main()
{
    scanf("%s",a1);
    scanf("%s",b1);
    if(a1[0]=='0' || b1[0]=='0')
    {
        cout<<0;
        return 0;
    }
    la=strlen(a1);
    lb=strlen(b1);
    for (i=0;i<=la-1;i++) a[la-i]=a1[i]-'0';
    for (i=0;i<=lb-1;i++) b[lb-i]=b1[i]-'0';
    for (i=1;i<=la;i++)
    {
        x=0;
        for (j=1;j<=lb;j++)
        {
            c[i+j-1]=a[i]*b[j]+c[i+j-1]+x;
            x=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
        c[i+j-1]=x;
    }
    lc=la+lb;
    while(c[lc]==0) lc--;
    for (i=lc;i>=1;i--) cout<<c[i];
    return 0;
}

|