20分求助

P1303 A*B Problem

ThChamp @ 2022-12-26 10:06:33

自己算都过了,提交就是20分下载数据后自己跑也过了,有没有大佬帮我看看

#include <cstdio>

int  a[50001], b[50001], c[50010];

int main() {
    int alen=0, blen=0; // 长度
    char ch = getchar();
    while(ch >= '0' && ch <='9') { // 快速读入两个数组
        a[alen++] = ch-'0';
        ch = getchar();
    }
    ch = getchar();
    while(ch >= '0' && ch <='9') {
        b[blen++] = ch-'0';
        ch = getchar();
    }
    for (int i = alen-1; i>=0; i--) // 错位相乘
        for (int j = blen-1; j>=0; j--)
            c[i+j+1]+=a[i]*b[j];

    for (int i = alen+blen-1; i>=1; i--) { // 计算进位
        c[i-1]+=c[i]/10;
        c[i]%=10;
    }
    int len = 0;
    while (c[len]==0 && len<alen+blen) { // 抛去废0
        len++;
    }
    if (len == alen+blen)
        printf("0");
    for (int i = len; i < alen+blen; i++) // 输出答案
        printf("%d", c[i]);
    return 0;
}

by Alone_Moonking @ 2022-12-26 10:52:55

#include<iostream>
#include<string>
using namespace std;
const int N = 100000000;
int a[N], b[N], c[N];
int main()
{
    string str1;
    string str2;
    cin >> str1 >> str2;
    for (int i = 0; i < str1.size(); i ++)
        a[str1.size()-1-i] = str1[i] - '0';
    for (int i = 0; i < str2.size(); i ++)
        b[str2.size()-1-i] = str2[i] - '0';
    for (int i = 0; i < str1.size(); i ++){
        for (int j = 0; j < str2.size(); j ++){
            c[j+i] += a[i] * b[j];
            c[j+1+i] += c[j+i] / 10;
            c[j+i] %= 10;
        }
    }int as = str1.size() + str2.size();
    while (c[as-1] == 0 && as > 1)
        as -= 1;
    for (int i = 0; i < as; i ++)
        cout << c[as-1-i];
    return 0;
}

by jnyz2021109122116 @ 2022-12-26 10:59:49

@ThChamp 在两次读入之间多加一个getchar()


by ThChamp @ 2022-12-26 15:43:22

@jnyz2021109122116 过了,感谢


|