BigInt 类型 共参考

cookiebus 2020-11-28 11:16:17 17 返回题目

#include <bits/stdc++.h>
using namespace std;

struct BigInt {
    int len, a[500];
    BigInt() {
        len = 1;
        a[1] = 0; 
        for (int i = 1; i < 500; ++i)
            a[i] = 0;
    };

    BigInt operator + (const BigInt &b) const {
        BigInt c;
        int l = len;
        if (b.len > l) l = b.len;

        for (int i = 1; i <= l; ++i) {
            c.a[i] += a[i] + b.a[i];
            c.a[i + 1] = c.a[i] / 10;
            c.a[i] = c.a[i] % 10;
        }
        if (c.a[l + 1] > 0) l ++;

        c.len = l;
        return c;
    }

    void init(int x) {
        len = 0;
        while (x > 0) {
            len ++;
            a[len] = x % 10;
            x /= 10;
        }
    }

    void print() {
        for (int i = len; i >= 1; i --)
            cout << a[i];
        cout << endl;
    }
};

BigInt a, b;
int main() {
    a.init(1890);
    b.init(19876541);
    BigInt c = a + b;
    c.print();
    return 0;
}
{{ vote && vote.total.up }}