20分求助!

P1303 A*B Problem

dazhuvss @ 2021-11-20 11:21:28

#include<iostream>  
#include<cstring>
char s[1001], ss[1001];
int a[1001], b[1001], c[1001];
using namespace std;
int main()
{
    cin >> s >> ss;
    int lens = strlen(s);
    int lenss = strlen(ss);
    for (int i = 1; i <= lens; i++) a[i] = s[lens - i] - '0';
    for (int i = 1; i <= lenss; i++)    b[i] = ss[lenss - i] - '0';
    for (int i = 1; i <= lenss; i++)
        for (int j = 1; j <= lens; j++) {
            c[i + j - 1] = a[j] * b[i];
        }
    for (int i = 1; i < lens + lenss; i++) {
        if (c[i] >9) {
            c[i + 1] +=  c[i] / 10;
            c[i] %= 10;
        }
    }
    int len = lens + lenss;
    while (c[len] == 0 && len > 1)  len--;
    for (int i = len; i >= 1; i--)  cout << c[i];
    return 0;
}

by Aslf_Ek @ 2021-11-20 11:41:51

2~4WA是因为一个位会被重复相加

c[i + j - 1] = a[j] * b[i];

改成

c[i + j - 1] += a[j] * b[i];

然后最后一个点RE是数组越界,把数组定大一点就行(加个0就行了)


|