美文网首页
PAT B1113 钱串子的加法

PAT B1113 钱串子的加法

作者: 梅友泥撑 | 来源:发表于2025-04-15 16:52 被阅读0次
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
    string a, b;
    vector<int> ans;
    int digit, carry = 0, i, j;
    cin >> a >> b;
    int len = a.length() > b.length() ? b.length() : a.length();
    reverse(a.begin(),a.end());
    reverse(b.begin(), b.end());
    for (i = 0; i < len; i++) {
        int sum = (a[i] - '0' < 10 ? a[i] - '0' : a[i] - 'a' + 10) + (b[i] - '0' < 10 ? b[i] - '0' : b[i] - 'a' + 10);
        sum += carry;
        digit = sum % 30;
        carry = sum / 30;
        ans.push_back(digit);
    }
    while (i < a.length()) {
        int sum = (a[i] - '0' < 10 ? a[i] - '0' : a[i] - 'a' + 10);
        sum += carry;
        digit = sum % 30;
        carry = sum / 30;
        ans.push_back(digit);
        i++;
    }
    while (i < b.length()) {
        int sum = (b[i] - '0' < 10 ? b[i] - '0' : b[i] - 'a' + 10);
        sum += carry;
        digit = sum % 30;
        carry = sum / 30;
        ans.push_back(digit);
        i++;
    }
    if (carry > 0) {
        ans.push_back(1);
    }
    while (ans[ans.size()-1] == 0) {
        ans.pop_back();
        if (ans.size() == 0) {
            cout << "0" << endl;
            return 0;
        }
    }
    reverse(ans.begin(), ans.end());
    for(auto it = ans.begin(); it != ans.end(); it++) {
        if ((*it) >= 10) {
            cout << (char)('a' + ((*it) - 10));
        }
        else {
            cout << (*it);
        }
    }
    cout << endl;
    return 0;
}

相关文章

  • 钱串子

    今天早上我发现一个钱串子,我和妈妈一开始以为蜈蚣呢! 妈妈说赶紧消灭掉它! 我说咱们一起喊救命好不好? 妈妈说那赶...

  • 钱串子

    元式催眠师 贾建莉 坚持分享492天 我和老公退休后,我们俩就改成每天吃两顿饭。早饭一般是老公做,中午饭是我来做。...

  • 迷失的“钱串子”

    静。心小憩片刻, 在这喧嚣的生活里, 人人都忙着去拜金去了, 是那么的忙乱, 不顾健康, 什么也不顾地拜金, 为的...

  • 钱串子的故事

    晚上回家进了楼道门,感应灯亮起的瞬间感觉楼道屋顶的白墙上有个黑点,定睛一看是一只“钱串子”。 “钱串子”学名“蚰蜒...

  • “钱串子”,加油!

    “钱串子”是一个人,是我身边“最”亲近的一个人,亲近到两个人已经在一起吃饭20多年。 缘分这东西很怪,在该来的时候...

  • “钱串子”的小温暖。

    钱串子是个多变性格的一个大男人,但同时也是生活中的大暖男。假如他看到我这篇文章,会觉得我对他的评价不公平。 我与他...

  • 钱和钱串子

    明代陆深在其笔记《春风堂随笔》记载这样一件事: “丘文庄公仲深濬,近世最号博学强记。洛阳刘少师希贤健尝...

  • 榆钱儿

    庭院里的榆树,每年春季都结满许多的榆钱儿,看上去真像一串串钱串子。挂满了榆树。榆钱儿不仅是钱串子,还是美味的食材。...

  • PAT-B 1095 解码PAT准考证(C语言)

    题目 链接:PAT (Basic Level) Practice 1095 解码PAT准考证 PAT 准考证号由 ...

  • 1141 PAT Ranking of Institutions

    After each PAT, the PAT Center will announce the ranking ...

网友评论

      本文标题:PAT B1113 钱串子的加法

      本文链接:https://www.haomeiwen.com/subject/iathbjtx.html