错误

P1303 A*B Problem

hanran_ @ 2021-07-06 08:57:16


#include <iostream>

using namespace std;

string x,y;
int a[100001],b[100001],c[100001],la,lb,lc;

int main () {
    cin >> x >> y;
    la = x.length();
    lb = y.length();
    for (int i = 0; i < la; i++) {
        a[la - i - 1] = x[i] - '0';
    }
    for (int i = 0; i < lb; i++) {
        b[lb - i - 1] = y[i] - '0';
    }

//  for (int i = 0; i < la; i++) {
//      cout << a[i];
//  }
    for (int j = 0; j < lb; j++) {
        for (int i = 0; i < la; i++) {
            c[i + j] += a[i] * b[j];
            c[i + j + 1] +=(c[i + j] / 10); 
            c[i + j] =c[i + j] % 10;
        }
    }

    lc = la + lb;

    for (int i = lc; i >= 1; i--) {
        if (c[i] != 0) {
            break;
        }
        lc--;
    }

    for (int i = lc; i > 0; i--) {
        cout << c[i];
    }

    return 0;
}

by GuaYsu @ 2021-07-27 19:17:50

输出的部分

for (int i = lc; i > 0; i--) {
        cout << c[i];
}

循环中间的i>0改成i>=0即可。您这样相当于没有输出个位。修改后代码即可AC

AC记录


|