-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1065.cpp
134 lines (129 loc) · 2.81 KB
/
1065.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
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <string>
#include <set>
#include <stack>
#include <cstdio>
#include <cstring>
#include <climits>
using namespace std;
string add(const string& a, const string& b) {
string res = "";
int i = 0, j = 0, sum = 0, carry = 0;
for (i = a.length() - 1, j = b.length() - 1; i >= 0 && j >= 0; i--, j--) {
sum = a[i] - '0' + b[j] - '0' + carry;
res += sum % 10 + '0';
carry = sum / 10;
}
while (i >= 0) {
sum = a[i] - '0' + carry;
res += sum % 10 + '0';
carry = sum / 10;
i--;
}
while (j >= 0) {
sum = b[j] - '0' + carry;
res += sum % 10 + '0';
carry = sum / 10;
j--;
}
if (carry != 0) res += carry + '0';
reverse(res.begin(), res.end());
return res;
}
string sub(const string& a, const string& b) {
string res = "";
int i = 0, j = 0, borrow = 0;
for (i = a.length() - 1, j = b.length() - 1; i >= 0 && j >= 0; i--, j--) {
if (a[i] - '0' - borrow < b[j] - '0') {
res += 10 + a[i] - '0' - borrow - (b[j] - '0') + '0';
borrow = 1;
}
else {
res += a[i] - '0' - borrow - (b[j] - '0') + '0';
borrow = 0;
}
}
while (i >= 0) {
if (a[i] - '0' - borrow < 0) {
res += 10 + a[i] - '0' - borrow + '0';
borrow = 1;
}
else {
res += a[i] - '0' - borrow + '0';
borrow = 0;
}
i--;
}
reverse(res.begin(), res.end());
i = 0;
while (res[i] == '0') i++;
return res.substr(i);
}
bool comp(const string& a, const string& b) {
if (a.length() == b.length()) {
return a < b;
}
else return a.length() < b.length();
}
bool operator< (const string& a, const string& b) {
if (a[0] == '-') {
if (b[0] == '-') {
return comp(b.substr(1), a.substr(1));
}
else return true;
}
else {
if (b[0] == '-') return false;
else return comp(a, b);
}
}
int main() {
string a, b, c, res;
int n;
cin >> n;
for (int i = 1; i <= n;i++) {
cin >> a >> b >> c;
if (a[0] == '-') {
if (b[0] == '-') {
res = "-" + add(a.substr(1), b.substr(1));
}
else {
if (a.substr(1) < b) {
res = sub(b, a.substr(1));
}
else if (a.substr(1) == b) res = "0";
else {
res = "-" + sub(a.substr(1), b);
}
}
}
else {
if (b[0] != '-') {
res = add(a, b);
}
else {
if (b.substr(1) < a) {
res = sub(a, b.substr(1));
}
else if (b.substr(1) == a) res = "0";
else {
res = "-" + sub(b.substr(1), a);
}
}
}
cout << "Case #" << i << ": "; // << res << endl;
if (c < res) {
cout << "true" << endl;
}
else cout << "false" << endl;
}
return 0;
}