-
Notifications
You must be signed in to change notification settings - Fork 4
/
CPPLAN02.cpp
72 lines (72 loc) · 1.41 KB
/
CPPLAN02.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
#include<bits/stdc++.h>
using namespace std;
string add(string a, string b){
string res="";
while(a.length() < b.length()) a="0"+a;
while(b.length() < a.length()) b="0"+b;
int carry=0;
for(int i=a.length()-1;i>=0;i--){
int tmp=a[i]-48 + b[i]-48 + carry;
carry=tmp/10;
tmp=tmp%10;
res=(char)(tmp+48)+res;
}
if(carry>0) res="1"+res;
return res;
}
string sub(string a, string b){
string res="";
while(a.length() < b.length()) a="0"+a;
while(b.length() < a.length()) b="0"+b;
bool neg=false;
if(a<b){
swap(a,b);
neg=true;
}
int borrow=0;
for(int i=a.length()-1; i>=0;i--){
int tmp=a[i]-b[i]-borrow;
if(tmp<0){
tmp+=10;
borrow=1;
}
else borrow=0;
res=(char)(tmp%10 + 48) + res;
}
//while(res.length()>1 && res[0]=='0') res.erase(0,1);
if(neg) res="-"+res;
return res;
}
void process(){
string a,b;
cin >> a >> b;
string res = "";
if (a[0] == '-' && b[0] == '-'){
res += "-";
res += add(a,b);
}
else if (a[0] != '-' && b[0] != '-'){
res = add(a,b);
}
else if (a[0] == '-' && b[0] != '-'){
res = sub(b,a);
}
else{
res = sub(a,b);
}
cout << res << endl;
}
int main(){
int T;
cin >> T;
while(T--){
process();
}
return 0;
}