测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A18253. 三数之和

填空题 较难

题目描述

三数之和

题目描述

给定三个非负整数 a,b,c,请你求出三个数的和。

输入格式

共三行,每行一个整数,依次代表 a、b、c。

输出格式

输出 a+b+c 的计算结果。

输入样例 #1

1
2
3

输出样例 #1

6

输入样例 #2

11111111111111111111111111111111111111111111111111111111111111111
22222222222222222222222222222222222222222222222222222222222222222
33333333333333333333333333333333333333333333333333333333333333333

输出样例 #2

66666666666666666666666666666666666666666666666666666666666666666

说明提示

0 ≤ a,b,c ≤ 101000

参考答案

#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; // 两个字符串大数相加 string add(string s1, string s2) { vector<int> res; int i = s1.size() - 1, j = s2.size() - 1, carry = 0; while (i >= 0 || j >= 0 || carry) { int a = i >= 0 ? s1[i--] - '0' : 0; int b = j >= 0 ? s2[j--] - '0' : 0; int sum = a + b + carry; carry = sum / 10; res.push_back(sum % 10); } string ans; reverse(res.begin(), res.end()); for (int num : res) ans += (char)('0' + num); return ans; } int main() { string a, b, c; cin >> a >> b >> c; string total = add(add(a, b), c); cout << total << endl; return 0; }
上一题 下一题