-
Notifications
You must be signed in to change notification settings - Fork 4
/
CPPLAN01.cpp
66 lines (65 loc) · 1.29 KB
/
CPPLAN01.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
#include<bits/stdc++.h>
using namespace std;
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;
}
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;
}
void process(){
string a,b;
cin >> a >> b;
string res = "";
if (b[0] == '-'){
res = add(a,b);
}else res = sub(a,b);
if (res[0] == '-') res.erase(res.begin(),res.begin()+1);
cout << res << endl;
}
int main(){
int T;
cin >> T;
while (T--)
{
process();
}
return 0;
}