-
Notifications
You must be signed in to change notification settings - Fork 1
/
aplusb2.cpp
141 lines (132 loc) · 3.32 KB
/
aplusb2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#include <queue>
#define ll long long
#define pii pair<int, int>
#define boost() cin.tie(0); cin.sync_with_stdio(0)
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
using namespace std;
int n;
int add;
int lenX, lenY, tmpX, tmpY, tmp, sub, del;
bool addNeg;
string one, two, rVar, sTmp;
string reverseStr(string str) {
int n = str.length();
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
return str;
}
string addition(string x, string y) {
string result = "";
int carry = 0;
lenX = x.size() - 1;
lenY = y.size() - 1;
while (lenX >= 0 || lenY >= 0) {
if (lenX < 0) {
tmpX = 0;
tmpY = y[lenY] - 48;
} else if (lenY < 0) {
tmpY = 0;
tmpX = x[lenX] - 48;
} else {
tmpX = x[lenX] - 48;
tmpY = y[lenY] - 48;
}
tmp = tmpX + tmpY + carry;
sTmp = to_string(tmp);
if (sTmp.size() > 1){
result += sTmp[1];
carry = 1;
} else {
carry = 0;
result += sTmp;
}
lenX -= 1;
lenY -= 1;
}
if (carry == 1) result += "1";
return reverseStr(result);
}
string subtraction(string x, string y) {
string result = "";
lenX = x.size() - 1;
lenY = y.size() - 1;
while (lenX >= 0 || lenY >= 0) {
if (lenX < 0) {
tmpX = 0;
tmpY = y[lenY] - 48;
} else if (lenY < 0) {
tmpY = 0;
tmpX = x[lenX] - 48;
} else {
tmpX = x[lenX] - 48;
tmpY = y[lenY] - 48;
}
tmp = tmpX - tmpY;
if (tmp < 0) {
int tmpX = lenX - 1;
while (tmpX >= 0 && x[tmpX] == '0') {
x[tmpX] += 9;
tmpX -= 1;
}
x[tmpX] -= 1;
tmp += 10;
}
result += to_string(tmp);
lenX -= 1;
lenY -= 1;
}
return reverseStr(result);
}
int main()
{
boost();
cin >> n;
for (int q = 0; q < n; q += 1) {
addNeg = 0;
cin >> one >> two;
add = 1;
if (one[0] == '-') {
add -= 1;
one.erase(0, 1);
sub = 1;
}
if (two[0] == '-') {
add -= 1;
two.erase(0, 1);
sub = 2;
}
if (add == 1) rVar = addition(one, two);
else if (add == -1) {
addNeg = 1;
rVar = addition(one, two);
}
else {
addNeg = 1;
if (sub == 1) {
swap(one, two);
}
if (two.length() > one.length()) rVar = subtraction(two, one);
else if (one.length() == two.length() && max(one, two) == two) {
rVar = subtraction(two, one);
}
else {
addNeg = 0;
rVar = subtraction(one, two);
}
}
del = 0;
while (del < rVar.length() && rVar[del] == '0') del += 1;
rVar.erase(0, del);
if (rVar == "") cout << 0 << endl;
else if (addNeg) cout << '-' << rVar << endl;
else cout << rVar << endl;
}
return 0;
}