求助

P1303 A*B Problem

Eat_A_HuTao @ 2020-08-29 00:29:29

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

by Eon_Sky @ 2020-08-29 06:45:52

建议将乘法和进位分开写,比较容易想


by Eon_Sky @ 2020-08-29 06:49:23

话说gets在洛谷不是不能用吗


by Eon_Sky @ 2020-08-29 06:50:31

@神剑 把gets改成cin就对了


by Eat_A_HuTao @ 2020-08-29 13:57:04

@键盘真香 好的谢谢


|