震惊!全ACGO最“短”题解
2025-10-05 15:05:41
发布于:浙江
0阅读
0回复
0点赞
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class BigInteger {
private:
vector<int> digits;
public:
BigInteger() {}
BigInteger(const string& s) {
for(int i = s.length() - 1; i >= 0; i--) {
digits.push_back(s[i] - '0');
}
}
BigInteger(long long n) {
if(n == 0) digits.push_back(0);
while(n > 0) {
digits.push_back(n % 10);
n /= 10;
}
}
BigInteger operator+(const BigInteger& other) const {
BigInteger result;
int carry = 0;
int max_len = max(digits.size(), other.digits.size());
for(int i = 0; i < max_len || carry; i++) {
int sum = carry;
if(i < digits.size()) sum += digits[i];
if(i < other.digits.size()) sum += other.digits[i];
result.digits.push_back(sum % 10);
carry = sum / 10;
}
return result;
}
string toString() const {
string s;
for(int i = digits.size() - 1; i >= 0; i--) {
s += char(digits[i] + '0');
}
return s;
}
};
int main() {
long long a, b;
cin >> a >> b;
// 使用大整数类(虽然没必要,但为了代码长度)
BigInteger big_a(a);
BigInteger big_b(b);
BigInteger result = big_a + big_b;
cout << result.toString();
return 0;
}
这里空空如也

有帮助,赞一个