大佬们走过路过来看看,为啥超时了啊,萌新求助

P1303 A*B Problem

realwzc @ 2021-06-17 20:57:19

大佬们走过路过来看看,为啥超时了啊,萌新求助

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

int s1[50] = {0};
int s2[50] = {0};
int s3[100] = {0};
int main()
{
    char a[50], b[50];
    int a1, a2, len;
    int p = 0;
    scanf("%s %s", a, b);
    a1 = strlen(a);
    a2 = strlen(b);

    for (int i = a1 - 1; i >= 0; i--)
    {
        s1[a1 - 1 - i] = a[i] - '0';
    }

    for (int i = a2 - 1; i >= 0; i--)
    {
        s2[a2 - 1 - i] = b[i] - '0';
    }

    for (int i = 0; i < a2; i++){
        for (int j = 0; j < a1;j++){
            s3[j+i] = s1[j] * s2[i] + s3[j+i];
            s3[j + 1 + i] = s3[j + i] / 10 + s3[j + 1 + i];
            s3[j+i] = s3[j+i] % 10;
        }
    }

    len = a1 + a2;
    while (s3[len]==0&&len>0)
    {
        --len;
    }

    for (int i = len ; i >= 0; i--)
    {
        printf("%d", s3[i]);
    }

    return 0;
}

by 晴天娃娃rt @ 2021-06-17 21:05:01

@realwzc 超时?


by realwzc @ 2021-06-17 21:06:31

@晴天娃娃rt runtime的问题,不过我把数组扩大到2000就都通过了。多谢


by yyandy @ 2021-06-17 21:07:23

@realwzc数组小了啊


by realwzc @ 2021-06-17 21:09:16

@yyandy 是的,多谢大佬


|