第39266题 组合题
阅读程序(第二题):高精度加法(含前导零输出)

本题输入的两个数均为非负整数,位数不超过 100000,可能包含前导零。

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int a[100007], b[100007], c[100007], carry[100007];
string input_str;
int a_len, b_len;
int main() {
  cin >> input_str;
  a_len = input_str.size();
  for (int i = 0; i < a_len; i++) {
    a[i] = input_str[a_len - i - 1] - '0';
  }
  cin >> input_str;
  b_len = input_str.size();
  for (int i = 0; i < b_len; i++) {
    b[i] = input_str[b_len - i - 1] - '0';
  }
  carry[0] = 0;
  for (int i = 0; i < max(a_len, b_len) + 1; i++) {
    c[i] = a[i] + b[i] + carry[i];
    if (c[i] >= 10) {
      carry[i + 1] = 1;
      c[i] -= 10;
    } else {
      carry[i + 1] = 0;
    }
  }
  for (int i = max(a_len, b_len); i >= 0; i--) {
    cout << c[i];
  }
  cout << endl;
  return 0;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
知识点:
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析