P1601题解

题意

就是A+B,相信大家都能理解,对于Python来说很简单,对C++崽就不友好了

解析

下面提供一种解法(代码中的BitInt500这个类,别问我那个什么int128函数,不顶事):
1.创建数组
2.从字符串构造大型整数
3.大整数加法(直接加肯定不行,用了逐位加法)
4.输出函数(只是懒得在程序中写影响美观)
注:*it为迭代器

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

__uint128_t a,b;
const int ma=001;

class BigInt500 {
public:
    vector<int> digits; // 存储每一位数字,digits[0] 为最低位
    bool is_negative;   // 新增:标记是否为负数

    BigInt500() : is_negative(false) {} // 默认构造函数

    // 从字符串构造大整数
    BigInt500(const string& num) : is_negative(false) {
        if (num.empty()) {
            throw invalid_argument("Input string is empty.");
        }
        int start = 0;
        if (num[0] == '-') {
            is_negative = true;
            start = 1;
            if (start >= num.size()) {
                throw invalid_argument("Input string is only a minus sign.");
            }
        }
        for (int i = num.size() - 1; i >= start; --i) {
            if (isdigit(num[i])) {
                digits.push_back(num[i] - '0');
            } else {
                throw invalid_argument("Input string contains non-digit characters.");
            }
        }
        // 去除前导零
        while (digits.size() > 1 && digits.back() == 0) {
            digits.pop_back();
        }
        // 如果数字是0,重置符号
        if (digits.empty() || (digits.size() == 1 && digits[0] == 0)) {
            is_negative = false;
        }
    }

    // 辅助函数:比较两个BigInt500的绝对值大小
    bool absGreaterOrEqual(const BigInt500& other) const {
        if (digits.size() != other.digits.size()) {
            return digits.size() > other.digits.size();
        }
        for (int i = digits.size() - 1; i >= 0; --i) {
            if (digits[i] != other.digits[i]) {
                return digits[i] > other.digits[i];
            }
        }
        return true; // 相等
    }

    // 大整数加法
    BigInt500 operator+(const BigInt500& other) const {
        // 如果符号相同,直接相加
        if (is_negative == other.is_negative) {
            BigInt500 result;
            result.is_negative = is_negative;
            int carry = 0;
            int maxSize = max(digits.size(), other.digits.size());
            
            for (int i = 0; i < maxSize || carry; ++i) {
                int digit1 = (i < digits.size()) ? digits[i] : 0;
                int digit2 = (i < other.digits.size()) ? other.digits[i] : 0;
                int sum = digit1 + digit2 + carry;
                carry = sum / 10;
                result.digits.push_back(sum % 10);
            }
            return result;
        } else {
            // 符号不同,转为减法
            if (absGreaterOrEqual(other)) {
                return *this - other;
            } else {
                BigInt500 temp = other - *this;
                temp.is_negative = !temp.is_negative;
                return temp;
            }
        }
    }

    // 大整数减法
    BigInt500 operator-(const BigInt500& other) const {
        // 如果符号不同,转为加法
        if (is_negative != other.is_negative) {
            BigInt500 temp = other;
            temp.is_negative = !temp.is_negative;
            return *this + temp;
        }
        
        BigInt500 result;
        // 如果被减数绝对值小于减数绝对值
        if (!absGreaterOrEqual(other)) {
            result = other - *this;
            result.is_negative = !result.is_negative;
            return result;
        }
        
        result.is_negative = is_negative;
        int borrow = 0;
        for (int i = 0; i < digits.size(); ++i) {
            int digit1 = digits[i] - borrow;
            int digit2 = (i < other.digits.size()) ? other.digits[i] : 0;
            if (digit1 < digit2) {
                digit1 += 10;
                borrow = 1;
            } else {
                borrow = 0;
            }
            result.digits.push_back(digit1 - digit2);
        }
        
        // 去除结果中的前导零
        while (result.digits.size() > 1 && result.digits.back() == 0) {
            result.digits.pop_back();
        }
        
        // 如果结果是0,重置符号
        if (result.digits.size() == 1 && result.digits[0] == 0) {
            result.is_negative = false;
        }
        
        return result;
    }

    // 输出大整数
    void print() const {
        if (is_negative && !(digits.size() == 1 && digits[0] == 0)) {
            cout << '-';
        }
        for (auto it = digits.rbegin(); it != digits.rend(); ++it) {
            cout << *it;
        }
        cout << endl;
    }
};

void read_uint128_t(__uint128_t &x) {
    string s;
    cin >> s;
    if (s.empty()) {
        cerr << "Error: Empty input string" << endl;
        return;
    }
    if (s.length() > 128) {
        cerr << "Error: Input string too long (exceeds 128 digits)" << endl;
        return;
    }
    for (char c : s) {
        if (!isdigit(c)) {
            cerr << "Error: Non-digit character in input" << endl;
            return;
        }
    }
    
    x = 0;
    for (int i = 0; i < s.size(); ++i) {
        x = x * 10 + (s[i] - '0');
    }
}

void write_uint128_t(__uint128_t x) {
    if (x == 0) {
        cout << '0';
        return;
    }
    string s;
    while (x > 0) {
        s.push_back((x % 10) + '0');
        x /= 10;
    }
    // 检查结果长度是否超过128位
    if (s.length() > 128) {
        cerr << "Error: Number too large to represent as 128-bit unsigned integer" << endl;
        return;
    }
    reverse(s.begin(), s.end());
    cout << s;
}

int main() {
    try {
        BigInt500 num1, num2;
        string str1, str2;
        cout << "Enter first number: ";
        cin >> str1;
        cout << "Enter second number: ";
        cin >> str2;
        
        num1 = BigInt500(str1);
        num2 = BigInt500(str2);
        
        BigInt500 diff = num1 - num2;
        cout << "Result: ";
        diff.print();
    } catch (const invalid_argument& e) {
        cerr << "Error: " << e.what() << endl;
        return 1;
    } catch (const exception& e) {
        cerr << "Unexpected error: " << e.what() << endl;
        return 1;
    }
    return 0;
}

后记

其实也不难,对吧
注:该代码可能会TLE,不过多试几遍肯定够能AC

posted @ 2025-10-24 21:00  LinuxUbuntu114  阅读(6)  评论(0)    收藏  举报