0分自测对为什么不过

P1303 A*B Problem

zxc2014722928 @ 2023-08-03 09:22:13

#include<bits/stdc++.h>
using namespace std;
int a[3000], b[3000], ans[3000], x;
int main() {
    string s1, s2;
    cin >> s1 >> s2;
    for (int i = 0; i < s1.size(); i++) a[s1.size() - i - 1] = s1[i] - '0';
    for (int i = 0; i < s2.size(); i++) b[s2.size() - i - 1] = s2[i] - '0';
    for (int i = 0; i < s1.size(); i++) {
        for (int j = 0; j < s2.size(); j++) {
            ans[i + j] = a[i] * b[j];
        }
    }
    int len = s1.size() + s2.size();
    for (int i = 0; i < len; i++) {
        ans[i] += x;
        if (ans[i] > 9) {
            x = ans[i] / 10;
            ans[i] %= 10;
        }
    }
    if (ans[len - 1] == 0)len--;
    for (int i = len-1; i >= 0; i--) {
        cout << ans[i];
    }
}

by 编码落寞 @ 2023-08-03 09:59:08

@zxc2014722928

  ans[i] += x;

这里加完x之后,需要把x清零,然后再判断


by zxc2014722928 @ 2023-08-03 10:49:56

@编码落寞 woc 谢谢哥


|