CPE.11448
unknown
c_cpp
2 years ago
1.6 kB
7
Indexable
#include <bits/stdc++.h>
using namespace std;
void sol(string a, string b) {
short aint[10001] = {0}, bint[10001] = {0}, result[10001] = {0};
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int aindex = 0, bindex = 0;
for (auto it : a) {
aint[aindex++] = it - '0';
}
for (auto it : b) {
bint[bindex++] = it - '0';
}
aindex--;bindex--;
bool flag = false;
int resultLen = max(aindex, bindex);
if (aindex <= bindex) {
for (int i = resultLen; i >= 0; i--) {
if (aint[i] != bint[i]) {
flag = (aint[i] < bint[i]);
cout << (flag ? "-" : "");
break;
}
}
}
if (flag) {
for (int i = 0; i <= bindex; i++) {
result[i] = bint[i] - aint[i];
if (result[i] < 0) {
result[i] += 10;
result[i++]--;
}
}
} else {
for (int i = 0; i <= aindex; i++) {
result[i] += aint[i] - bint[i];
if (result[i] < 0) {
result[i] += 10;
result[i + 1]--;
}
}
}
while(resultLen> 0&& !result[resultLen]) --resultLen;
while(resultLen>=0) cout << result[resultLen--];
}
int main(int argc, char const *argv[]) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
string a, b;
int n;
cin >> n;
for(; n>0; n--){
cin >> a >> b;
sol(a, b);
cout << endl;
}
return 0;
}Editor is loading...