AcWing 3702. 十六进制不进位加法
原题链接
简单
作者:
馍高一丈
,
2024-09-29 21:36:38
,
所有人可见
,
阅读 1
#include <iostream>
#include <vector>
using namespace std;
vector<int> A, B, C;
void deduct_0 (vector<int> &k){
while(k.size() > 1 && k.back() == 0) k.pop_back();
return;
}
vector<int> add (vector<int> a, vector<int> b){
vector<int> c;
int tmp = 0;
for(int i = 0; i < a.size() || i < b.size(); i ++){
if (i < a.size()) tmp += a[i];
if (i < b.size()) tmp += b[i];
c.push_back(tmp % 16);
tmp = 0;
}
return c;
}
int main() {
string a, b;
while (cin >> a >> b) {
for (int i = a.size() - 1; i >= 0; i --) {
if (a[i] >= '0' && a[i] <= '9')
A.push_back(a[i] - '0');
else
A.push_back(a[i] - 'A' + 10);
}
deduct_0 (A);
for (int i = b.size() - 1; i >= 0; i --) {
if (b[i] >= '0' && b[i] <= '9')
B.push_back(b[i] - '0');
else
B.push_back(b[i] - 'A' + 10);
}
deduct_0 (B);
C = add (A, B);
for(int i = C.size() - 1; i >= 0; i --){
if (C[i] >= 10 && C[i] <= 15)
printf("%c", C[i] - 10 + 'A');
else
printf("%d", C[i]);
}
cout << endl;
A.clear ();
B.clear ();
C.clear ();
}
return 0;
}