UVaOJ 424 - Integer Inquiry


—— by A Code Rabbit


Description

给一系列数,计算这些数的总和。

数字比较大,高可达100位。


Type

Big Number


Analysis

典型高精度加法。

直接套用高精度的模板来做。

貌似也没什么好说的。

代码虽长,但都是复制粘贴的渣渣。


Solution

// UVaOJ 424
// Integer Inquiry
// by A Code Rabbit

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAXD = 1000;

struct BigUnsigned {
    int len, d[MAXD];
    BigUnsigned() { memset(d, 0, sizeof(d)); len = 0; }
    BigUnsigned(int num) { *this = num; }
    BigUnsigned(const char* str) { *this = str; }
    void Clean() { while (len > 1 && !d[len - 1]) len--; }
    BigUnsigned operator=(int num) {
        char str[MAXD];
        sprintf(str, "%d", num);
        *this = str;
        return *this;
    }
    BigUnsigned operator=(const char* str) {
        memset(d, 0, sizeof(d));
        len = strlen(str);
        for (int i = 0; i < len; i++) d[i] = str[len - i - 1] - '0';
        Clean();
        return *this;
    }

    string ToStr() const {
        string res = "";
        for (int i = 0; i < len; i++) res = (char)(d[i] + '0') + res;
        return res == "" ? "0" : res;
    }
    friend istream& operator>>(istream &in, BigUnsigned& one) {
        string str;
        in >> str;
        one = str.c_str();
        return in;
    }
    friend ostream& operator<<(ostream &out, const BigUnsigned& x) {
        out << x.ToStr();
        return out;
    }

    BigUnsigned operator+(const BigUnsigned& one) const {
        BigUnsigned res;
        for (int i = 0, x = 0; i < max(len, one.len) + 1; i++) {
            int tmp = d[i] + one.d[i] + x;
            res.d[res.len++] = tmp % 10;
            x = tmp / 10;
        }
        res.Clean();
        return res;
    }

    BigUnsigned operator+=(const BigUnsigned& one) { return *this = *this + one; }


    bool operator<(const BigUnsigned& one) const {
        if (len != one.len) return len < one.len;
        for (int i = len - 1; i >= 0; i--)
            if (d[i] != one.d[i]) return d[i] < one.d[i];
        return false;
    }
    bool operator!=(const BigUnsigned& one) const { return one < *this || *this < one; }
};

BigUnsigned ans = 0;
BigUnsigned one;

int main() {
    while (cin >> one && one != 0) {
        ans += one;
    }
    cout << ans << endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值