第一个为啥WA 80分求大佬解答

P1303 A*B Problem

huangyuming @ 2021-02-01 16:10:39


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

int main()
{
    char s1[9999999],s2[9999999];
    int a[9999999]={0},b[9999999]={0},c[9999999]={0};
    int la,lb,lc,i,j;

    scanf("%s",s1);
    scanf("%s",s2);

    la=strlen(s1);
    lb=strlen(s2);

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

    lc=la+lb;
    for(i=1;i<=la;i++)
    {
        for(j=1;j<=lb;j++)
        {
            c[i+j-1]+=a[i]*b[j];
            c[i+j]+=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
    }

    if(c[lc]==0 && lc>0) lc--;
    for(i=lc;i>0;i--)
    {
        printf("%d",c[i]);
    }
    return 0;
}

by EuphoricStar @ 2021-02-01 16:15:10

@huangyuming 数组尽量定义为全局变量

#include<bits/stdc++.h>
using namespace std;
char s1[9999999],s2[9999999];
int a[9999999]={0},b[9999999]={0},c[9999999]={0};
int main()
{
    int la,lb,lc,i,j;

    scanf("%s",s1);
    scanf("%s",s2);

    la=strlen(s1);
    lb=strlen(s2);

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

    lc=la+lb;
    for(i=1;i<=la;i++)
    {
        for(j=1;j<=lb;j++)
        {
            c[i+j-1]+=a[i]*b[j];
            c[i+j]+=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
    }

    while(c[lc]==0 && lc>1) lc--;  // 修改
    for(i=lc;i>0;i--)
    {
        printf("%d",c[i]);
    }
    return 0;
}

by huangyuming @ 2021-02-01 16:24:51

@zltzlt 大佬能给个解释吗?谢谢


by Anonymous_NULL @ 2021-02-01 17:30:54

@huangyuming

开全局变量可以:


by huangyuming @ 2021-02-01 20:35:33

@Anonymous_NULL 是修改的部分不是数组


|