60分。最后一个测试点RE,倒数第二个测试点wa

P1303 A*B Problem

rsrs @ 2022-12-06 21:42:15


#include<string.h>
char a[250], b[250];
int A[250], B[250], C[500];
int main()
{
    int i, j, n, k;
    scanf_s("%s", a,250);
    scanf_s("%s", b,250);
    for (i = 0; i < strlen(a); i++)
        A[strlen(a) - i - 1] = a[i] - '0';
    for (i = 0; i < strlen(b); i++)
        B[strlen(b) - i - 1] = b[i] - '0';
    for (i = 0; i < strlen(a); i++)
    {
        for (j = 0; j < strlen(b); j++)
        {
            k = i + j;
            C[k] = A[i] * B[j]+C[k];
            if (C[k] >= 10)
            {
                C[k + 1] = C[k + 1] + C[k] / 10;
                C[k] = C[k] % 10;
            }
        }
    }
    n = strlen(a) + strlen(b)+1;
    if (C[n] > 0)
        n++;
    while (C[n - 1] == 0 && n > 1)n--;
    for (i = n - 1; i >= 0; i--)
        printf("%d",C[i]);
    return 0;
}

by Sad_Rex @ 2022-12-06 22:02:46

@renshuo654321 数组开小了


by Sad_Rex @ 2022-12-06 22:05:17


#include<bits/stdc++.h>
using namespace std;
char a[2500], b[2500];
int A[2500], B[2500], C[5000];
int main()
{
    int i, j, n, k;
    cin >> a >> b;
    for (i = 0; i < strlen(a); i++)
        A[strlen(a) - i - 1] = a[i] - '0';
    for (i = 0; i < strlen(b); i++)
        B[strlen(b) - i - 1] = b[i] - '0';
    for (i = 0; i < strlen(a); i++)
    {
        for (j = 0; j < strlen(b); j++)
        {
            k = i + j;
            C[k] = A[i] * B[j]+C[k];
            if (C[k] >= 10)
            {
                C[k + 1] = C[k + 1] + C[k] / 10;
                C[k] = C[k] % 10;
            }
        }
    }
    n = strlen(a) + strlen(b)+1;
    if (C[n] > 0)
        n++;
    while (C[n - 1] == 0 && n > 1)n--;
    for (i = n - 1; i >= 0; i--)
        printf("%d",C[i]);
    return 0;
}

by Sad_Rex @ 2022-12-06 22:05:50

重声:我是c++oier


by rsrs @ 2022-12-06 22:17:21

@1bigRex_zkr 谢!我没看到题目是2000位


|