萌新求助!!最后一个点re T - T

P1303 A*B Problem

Noldo @ 2024-01-22 18:17:13

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

string a, b;
struct h {
    int num[20010] = { 0 };
    int len = 0;
};
h ans;
h stringtoint(string s) {
    h ans;
    for (int i = s.size() - 1; i >= 0; i--)
        ans.num[ans.len++] = s[i] - '0';
    return ans;
}
h cal(h a, h b) {
    h ans;
    ans.len = a.len + b.len - 1;
    for (int i = 0; i <= a.len - 1; i++) {
        for (int j = 0; j <= b.len - 1; j++) {
            ans.num[i + j] += a.num[i] * b.num[j];
            ans.num[i + j + 1] += ans.num[i + j] / 10;
            ans.num[i + j] = ans.num[i + j] % 10;
        }
    }
    while (ans.num[ans.len + 1] > 0)ans.len++;
    return ans;
}
int print(h ans) {
    for (int i = ans.len - 1; i >= 0; i--) {
        printf("%d", ans.num[i]);
        if (ans.num[ans.len - 1] == 0)return 0;
    }
    return 0;
}
int main() {
    cin >> a >> b;

    h n1 = stringtoint(a);
    h n2 = stringtoint(b);

    print(cal(n1, n2));
    return 0;
}

by Kevin626 @ 2024-01-23 17:43:54

while加判断是否超过范围“while(ans.num[ans.len + 1] > 0)ans.len++;”


|