洛谷B2001 入门测试题目

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值