1949 Problem A a+b

389 篇文章 1 订阅
144 篇文章 2 订阅

问题 A: a+b
时间限制: 1 Sec 内存限制: 32 MB
献花: 76 解决: 44
[献花][花圈][TK题库]
题目描述
实现一个加法器,使其能够输出a+b的值。
输入
输入包括两个数a和b,其中a和b的位数不超过1000位。
输出
可能有多组测试数据,对于每组数据,
输出a+b的值。
样例输入
6 8
2000000000 30000000000000000000
样例输出
14
30000000002000000000

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

const int MaxN = 100001;


int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG


    string s1, s2, res;
    int s1L, s2L,L;
    int left = 0;
    while (cin >> s1 >> s2)
    {
        L = 0;
        res.clear();
        left = 0;
        reverse(s1.begin(), s1.end());
        reverse(s2.begin(), s2.end());
        s1L = s1.length();
        s2L = s2.length();

        while (L < s1L && L < s2L)
        {
            res += (s1[L] + s2[L] + left - '0' * 2) % 10 + '0';
            left = (s1[L] + s2[L] + left - '0' * 2) / 10;
            ++L;
        }

        while (L < s1L)
        {
            res += (s1[L] + left - '0') % 10 + '0';
            left = (s1[L] + left - '0') / 10;
            ++L;
        }

        while (L < s2L)
        {
            res += (s2[L] + left - '0') % 10 + '0';
            left = (s2[L] + left - '0') / 10;
            ++L;
        }
        if (left)
            res += left + '0';
        reverse(res.begin(), res.end());
        printf("%s\n", res.c_str());
    }


    return 0;
}
/**************************************************************
    Problem: 1949
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:2 ms
    Memory:1708 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值