求助大佬!第五个测试点过不了!

P1303 A*B Problem

lv123123 @ 2024-02-18 17:35:33

#include<stdio.h>
#include<string.h>
int main()
{
    // 输入的两个乘数; 
    char x[1000],y[1000];
    scanf("%s%s", x, y );
    // 数组储存; 
    int a[100000] = {0}, b[100000] = {0}, c[100000] = {0};
    int lena, lenb;
    // 求出乘数的长度; 
    lena = strlen(x);
    lenb = strlen(y);
    // 字符串转数组(下标反向); 
    for ( int i = 0; i < lena; i++ ) {
        a[lena - i-1] = x[i] - '0';
    }
    for ( int i = 0; i < lenb; i++ ) {
        b[lenb - i-1] = y[i] - '0';
    }
    // 乘完的结果至少是多少位?
    int len = lena + lenb - 1;
    // 两数相乘; 
    for ( int i = 0; i < lena; i++ ) {
        for ( int j = 0; j < lenb; j++ ) {
            c[i+j] += a[i]*b[j];
        }
    }
    // 两数相乘后,每行相加: 
    for ( int i = 0; i < len; i++ ) {
        // 进位; 
        if ( c[i] >= 10 ) {
            c[i+1] += c[i]/10;
            c[i] %= 10;
        }
    }
    // 消除前导的'0'; 
    while ( c[len] == 0 && len >= 1 ) len--;
    // 逆向输出; 
    for ( int i = len; i >= 0; i-- ) {
        printf("%d", c[i] );
    }
    return 0;
}

by lihaoyu114514 @ 2024-02-18 17:55:05

a=input() b=input() a=int(a) b=int(b) print(a*b)


by hei_ke @ 2024-02-18 19:14:46

a=input() b=input() a=int(a) b=int(b) print(a*b)


by wuzhiyuan123 @ 2024-02-21 20:51:32

两位大佬楼主用的是c++^-^


by wuzhiyuan123 @ 2024-02-21 20:54:41

@lv123123 你的char数组x,y开小了, 建议你每次开空间的时候都用一个固定的数并把数组开到主函数外面,这样可使用的空间会更大,每次这个固定的数一定要大,建议使用1000010,也就是1e6+10


by wuzhiyuan123 @ 2024-02-21 20:56:05

si,不对啊,楼主用的是c语言吧?


by lv123123 @ 2024-02-22 14:26:39

@wuzhiyuan123 谢谢,确实是char数组开小了,通过啦!


|