#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
std::string add (const std::vector<int>& a, const std::vector<int>& b) {
std::string ans = "";
int carry = 0;
int i = 0;
for (; i < std::min((int)a.size(), (int)b.size()); ++i) {
carry += (a[i] + b[i]);
ans += (std::to_string(carry % 10));
carry /= 10;
}
while (i < (int)a.size()) {
carry += a[i++];
ans += (std::to_string(carry % 10));
carry /= 10;
}
while (i < (int)b.size()) {
carry += b[i++];
ans += (std::to_string(carry % 10));
carry /= 10;
}
if (carry) {
ans += "1";
}
std::reverse(ans.begin(), ans.end());
return ans;
}
int main() {
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
std::string a, b;
std::vector<int> vecA, vecB;
std::cin >> a >> b;
for (size_t i = 0; i < a.size(); ++i) {
vecA.push_back((int)(a[i] - '0'));
}
for (size_t i = 0; i < b.size(); ++i) {
vecB.push_back((int)(b[i] - '0'));
}
std::reverse(vecA.begin(), vecA.end());
std::reverse(vecB.begin(), vecB.end());
std::cout << add(vecA, vecB) << std::endl;
return 0;
}
洛谷B2001 入门测试题目
最新推荐文章于 2024-08-22 19:32:10 发布