forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
128 lines (102 loc) · 3.27 KB
/
main.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
/// Source : https://leetcode.com/problems/equal-rational-numbers/
/// Author : liuyubobobo
/// Time : 2018-01-05
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
/// Implement a Fraction class
/// For rational equal, try to compare the two fraction string
/// or adding 0.000...0001 to the smaller fraction and compare again
///
/// Time Complexity: O(1)
/// Space Complexity: O(1)
class Fraction{
private:
const int n = 100;
string integer_part, decimal_part;
public:
Fraction(const string& s){
int dot = s.find('.');
if(dot == string::npos){
integer_part = s;
decimal_part = string(n, '0');
}
else{
integer_part = s.substr(0, dot);
string t = s.substr(dot + 1);
int l = t.find('(');
if(l == string::npos)
decimal_part = t + string(n - t.size(), '0');
else{
assert(t.back() == ')');
decimal_part = t.substr(0, l);
string repeat_part = t.substr(l + 1, t.size() - decimal_part.size() - 2);
while(decimal_part.size() <= n)
decimal_part += repeat_part;
decimal_part = decimal_part.substr(0, n);
}
}
}
bool rational_equal(Fraction& another){
if(*this == another)
return true;
if(*this < another){
Fraction x = this->add_a_bit();
return x == another;
}
Fraction x = another.add_a_bit();
return *this == x;
}
Fraction add_a_bit(){
int carry = 0;
string new_decimal_part = add_one(decimal_part, carry);
string new_integer_part = integer_part;
if(carry){
carry = 0;
new_integer_part = add_one(integer_part, carry);
if(carry) new_integer_part = "1" + new_integer_part;
}
return Fraction(new_integer_part + "." + new_decimal_part);
}
bool operator==(const Fraction& another){
return this->integer_part == another.integer_part &&
this->decimal_part == another.decimal_part;
}
bool operator<(const Fraction& another){
return this->integer_part < another.integer_part ||
(this->integer_part == another.integer_part &&
this->decimal_part < another.decimal_part);
}
private:
string add_one(const string& s, int& ret_carry){
string res = s;
assert(res.size() > 0);
res[res.size() - 1] += 1;
int carry = 0;
for(int i = (int)res.size() - 1; i >= 0; i --){
int num = res[i] - '0' + carry;
res[i] = '0' + num % 10;
carry = num / 10;
}
ret_carry = carry;
return res;
}
};
class Solution {
public:
bool isRationalEqual(string S, string T) {
Fraction s = Fraction(S), t = Fraction(T);
return s.rational_equal(t);
}
};
int main() {
string S1 = "0.(52)", T1 = "0.5(25)";
cout << Solution().isRationalEqual(S1, T1) << endl;
string S2 = "0.1666(6)", T2 = "0.166(66)";
cout << Solution().isRationalEqual(S2, T2) << endl;
string S3 = "0.9(9)", T3 = "1.";
cout << Solution().isRationalEqual(S3, T3) << endl;
return 0;
}