題目描述
高精度加法,相當于 a+b problem,不用考慮負數。
輸入格式
分兩行輸入。a,b≤10500a,b \leq 10^{500}a,b≤10500。
輸出格式
輸出只有一行,代表 a+ba+ba+b 的值。
輸入輸出樣例 #1
輸入 #1
1
1
輸出 #1
2
輸入輸出樣例 #2
輸入 #2
1001
9099
輸出 #2
10100
說明/提示
20%20\%20% 的測試數據,0≤a,b≤1090\le a,b \le10^90≤a,b≤109;
40%40\%40% 的測試數據,0≤a,b≤10180\le a,b \le10^{18}0≤a,b≤1018。
solution
用數組表示,模擬手算加法的逐位計算考慮進位即可
代碼
#include "iostream"
#include "math.h"
#include "algorithm"
#include "string.h"
#include "unordered_set"
#include "deque"
#include "stack"using namespace std;
const int N = 5e2 + 5;string a, b;
int c[N];int main() {cin >> a >> b;reverse(a.begin(), a.end());for (int i = 0; i < a.size(); i++) c[i] = a[i] - '0';reverse(b.begin(), b.end());for (int i = 0; i < b.size(); i++) c[i] += b[i] - '0';int s = 0;for (int & i : c) i += s, s = i / 10, i %= 10;int i = N - 1;while (i > 0 && c[i] == 0) i--;for (int j = i; j >= 0; j--)cout << c[j];return 0;
}