ja1u0va @ 2024-12-09 19:51:52
下面这段是AC代码
#include <iostream>
#include <cstring>
#define LEN 4100
void clear(int a[])
{
for (int i = 0; i < LEN; ++i)
a[i] = 0;
}
void read(int a[])
{
char s[LEN + 1];
scanf("%s", s);
clear(a);
int len = strlen(s);
for (int i = 0; i < len; ++i)
a[len - i - 1] = s[i] - '0';
}
void write(int a[])
{
int i;
for (i = LEN - 1; i >= 1; --i)
if (a[i] != 0)
break;
for (; i >= 0; --i)
printf("%d", a[i]);
}
void mul(int a[], int b[], int c[])
{
clear(c);
for (int i = 0; i < LEN - 1; ++i)
{
for (int j = 0; j <= i; ++j)
c[i] += a[j] * b[i - j];
if (c[i] >= 10)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
}
int main()
{
int a[LEN];
int b[LEN];
int c[LEN];
read(a);
read(b);
mul(a, b, c);
// write(a);
// printf("\n");
// write(b);
// printf("\n");
write(c);
return 0;
}
但是如果把read函数改为
clear(a);
int i = 0;
int temp;
char ch = getchar();
while (ch >= '0' && ch <= '9')
{
a[i] = ch - '0';
++i;
ch = getchar();
}
for (int j = 0; j < i / 2; j++)
{
temp = a[j];
a[j] = a[i - j - 1];
a[i - j - 1] = temp;
}
后面几个就都会WA,并且都是第一行第一列为0,调了好久,不清楚是为什么,在本地输出结果也是对的,看不出来问题,求大佬帮助