forked from Krish03na/CS204
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifference.cpp
61 lines (47 loc) · 1002 Bytes
/
difference.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
#include <bits/stdc++.h>
using namespace std;
bool smaller(string a, string b){
int l1 = a.length();
int l2 = b.length();
if(l1<l2) return true;
if(l2>l1) return false;
for(int i=0; i<l1; i++){
if(a[i]<b[i]) return true;
else if(a[i]> b[i]) return false;
else return false;
}
}
string Diff(string a, string b){
if(smaller(a, b)) swap(a, b);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int l1 = a.length();
int l2 = b.length();
string ans = "";
int diff, carry=0;
for(int i=0; i< l2; i++){
diff = (a[i]- '0') - (b[i]- '0');
if(diff<0) {
diff +=10;
a[i+1]--;
}
ans.push_back(diff+'0');
}
for(int i=l2; i< l1; i++){
diff = (a[i]- '0');
ans.push_back(diff + '0');
}
reverse(ans.begin(), ans.end());
return ans;
}
int main(){
int T;
cin >>T;
while(T--){
string a;
cin >> a;
string b;
cin >> b;
cout << Diff(a, b) << endl;}
return 0;
}