这个代码怎么是0分

P1303 A*B Problem

XiongYingHao @ 2023-10-13 21:08:25

#include <stdio.h>
#include <string.h>

char x[5005], y[5005];
int a[5005], b[5005], c[5005], la, lb, lc;
int main()
{
    gets(x);
    gets(y);
    la = strlen(x);
    lb = strlen(y);
    for (int i = 0; i < la; i++)
        a[la - i] = x[i] - '0';
    for (int i = 0; i < lb; i++)
        b[lb - i] = y[i] - '0';
    for (int i = 0; i <= la; i++)
    {
        for (int j = 0; j <= lb; j++)
        {
            c[i + j - 1] += a[i] * b[j];
            c[i + j] = c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
    }
    lc = la + lb;
    if (c[lc + 1] > 0)
        lc++;
    while (c[lc] == 0 && lc > 1)
        lc--;
    for (int i = lc; i > 0; i--)
        printf("%d", c[i]);
    return 0;
}

by genshin202729852 @ 2023-10-14 09:45:14


#include <stdio.h>
#include<iostream> 
#include <string.h>
using namespace std; 
char x[50005], y[50005];
int a[50005], b[50005], c[50005], la, lb, lc;
int main()
{
    cin>>x;
    cin>>y;
    la = strlen(x);
    lb = strlen(y);
    for (int i = 0; i < la; i++)
        a[la - i] = x[i] - '0';
    for (int i = 0; i < lb; i++)
        b[lb - i] = y[i] - '0';
    for (int i = 0; i <= la; i++)
    {
        for (int 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;
        }
    }
    lc = la + lb;
    if (c[lc + 1] > 0)
        lc++;
    while (c[lc] == 0 && lc > 1)
        lc--;
    for (int i = lc; i > 0; i--)
        printf("%d", c[i]);
    return 0;
}```
 你的代码,问题主要是i+j-1在原来的代码里会过0,导致进位出错,还有进位是+=,直接用=会导致原本的进位被后来的进位覆盖

by genshin202729852 @ 2023-10-14 09:47:04

还有数组要开大,可能会爆,因为是乘法


|