【洛谷】P1601 A+B Problem(高精)

题目地址:

https://www.luogu.com.cn/problem/P1601

题目描述
高精度加法,相当于a+b problem,不用考虑负数.

输入格式:
分两行输入。 a , b ≤ 1 0 500 a,b \leq 10^{500} a,b10500

输出格式:
输出只有一行,代表 a + b a+b a+b的值

高精度加法。以字符串的方式输入,手工模拟整数加法就可以了。注意最后要判断最高位是否有进位。代码如下:

#include <algorithm>
#include <iostream>
using namespace std;

string res;
string s1, s2;

int main() {
  cin >> s1;
  cin >> s2;

  reverse(s1.begin(), s1.end());
  reverse(s2.begin(), s2.end());

  int t = 0;
  for (int i = 0; i < s1.size() || i < s2.size(); i++) {
    if (i < s1.size()) t += s1[i] - '0';
    if (i < s2.size()) t += s2[i] - '0';
    res += '0' + t % 10;
    t /= 10;
  }

  if (t) res += '1';

  reverse(res.begin(), res.end());
  cout << res << endl;
}

时空复杂度 O ( max ⁡ { l a , l b } ) O(\max\{l_a,l_b\}) O(max{la,lb})

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值