为什么cpp里是对的,luogu里就错了

P1303 A*B Problem

幻空 @ 2020-02-27 21:31:19

下了个测试点 12345 65432 输出 807758040
我的答案在自己的c++上输出的对的,但是luogu评测不知道怎么就变成20分了,错的都是输出0

#include <bits/stdc++.h>

using namespace std;
int a[2010],b[2010],f[4010],t,la,lb,l;
char c;
int main()
{
    c=getchar();
    while (c>='0'&&c<='9')
    {
        la++;
        a[la]=(int) (c-'0');
        c=getchar();
    }
    c=getchar();
    while (c>='0'&&c<='9')
    {
        lb++;
        b[lb]=(int) (c-'0');
        c=getchar();
    }

    for (int i=1;i<=la/2;i++)
    {
        t=a[la-i+1];
        a[la-i+1]=a[i];
        a[i]=t;
    }
    for (int i=1;i<=lb/2;i++)
    {
        t=b[lb-i+1];
        b[lb-i+1]=b[i];
        b[i]=t;
    }

    for (int i=1;i<=la;i++)
      for (int j=1;j<=lb;j++)
      {
        f[i+j-1]+=a[i]*b[j];
        if (f[i+j-1]>9)
        {
            f[i+j]+=f[i+j-1]/10;
            f[i+j-1]%=10;
        }
      }

    l=la+lb+1;
    while (f[l]==0&&l>1) l--;

    for (int i=l;i>=1;i--)
      cout<<f[i];
}

by Fading @ 2020-02-27 21:32:56

试试luoguIDE


by 幻空 @ 2020-02-27 21:38:50

@Fading 评测机里输出的答案也是对的


by Retired_lvmao @ 2020-02-27 21:45:29

@ALB教皇

换行符可能是'\r\n',需要读两个换行符


by 幻空 @ 2020-02-27 21:48:05

@lv_mao_da_lao 谢了,就是这个问题,感谢大佬


by 幻空 @ 2020-02-27 21:48:42

发一下改正过后的代码吧,希望后面的人可以不要被这个坑了

#include <bits/stdc++.h>

using namespace std;
int a[2010],b[2010],f[4010],t,la,lb,l;
char c;
int main()
{
    c=getchar();
    while (c>='0'&&c<='9')
    {
        la++;
        a[la]=(int) (c-'0');
        c=getchar();
    }
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9')
    {
        lb++;
        b[lb]=(int) (c-'0');
        c=getchar();
    }

    for (int i=1;i<=la/2;i++)
    {
        t=a[la-i+1];
        a[la-i+1]=a[i];
        a[i]=t;
    }
    for (int i=1;i<=lb/2;i++)
    {
        t=b[lb-i+1];
        b[lb-i+1]=b[i];
        b[i]=t;
    }

    for (int i=1;i<=la;i++)
      for (int j=1;j<=lb;j++)
      {
        f[i+j-1]+=a[i]*b[j];
        if (f[i+j-1]>9)
        {
            f[i+j]+=f[i+j-1]/10;
            f[i+j-1]%=10;
        }
      }

    l=la+lb+1;
    while (f[l]==0&&l>1) l--;

    for (int i=l;i>=1;i--)
      cout<<f[i];
}

by KentGu @ 2020-02-28 21:10:29

【是什么问题啊 】


by KentGu @ 2020-02-28 21:10:48

我以往都是因为数组越界了


|