7-110 3201 A+B问题

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <numeric>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using pii = std::pair<i32, i32>;
using pll = std::pair<i64, i64>;
using vi = std::vector<i32>;
using vll = std::vector<i64>;
using vpii = std::vector<pii>;
using vpll = std::vector<pll>;

void solve();

int main()
{
    std::cin.tie(nullptr)->sync_with_stdio(false);
    solve();
}

bool compare(i32 a, i32 b, i32 k)
{
    std::string s1{std::to_string(a)}, s2{std::to_string(b)};
    i32 count{0};
    while (!s1.empty() && !s2.empty())
    {
        if (s1.back() == s2.back())
        {
            ++count;
            s1.pop_back();
            s2.pop_back();
        }
        else
        {
            return count == k;
        }
    }
    while (!s1.empty())
    {
        if (s1.back() == '0')
        {
            ++count;
        }
        s1.pop_back();
    }
    while (!s2.empty())
    {
        if (s2.back() == '0')
        {
            ++count;
        }
        s2.pop_back();
    }
    return count == k;
}

void solve()
{
    i32 a, b, k;
    bool endl = false;
    while (std::cin >> a >> b >> k)
    {
        if (a == 0 && b == 0)
        {
            return;
        }
        if (endl)
        {
            std::cout << '\n';
        }
        if (compare(a, b, k))
        {
            std::cout << -1;
        }
        else
        {
            std::cout << a + b;
        }
        endl = true;
    }
}
posted @ 2025-10-13 13:30  TPPPP72  阅读(5)  评论(0)    收藏  举报