forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
80 lines (62 loc) · 1.89 KB
/
main2.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
/// Source : https://leetcode.com/problems/equal-rational-numbers/
/// Author : liuyubobobo
/// Time : 2018-01-06
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <cmath>
using namespace std;
/// Implement a Fraction class
/// Using geometric sum to deal with the repeat part :-)
///
/// Time Complexity: O(1)
/// Space Complexity: O(1)
class Fraction{
private:
const double e = 1e-8;
double x = 0.0;
public:
Fraction(const string& s){
int dot = s.find('.');
if(dot == string::npos)
x = atoi(s.c_str());
else{
x = atoi(s.substr(0, dot).c_str());
string t = s.substr(dot + 1);
int l = t.find('(');
if(l == string::npos)
x += (double)atoi(t.c_str()) * pow(0.1, t.size());
else{
assert(t.back() == ')');
string decimal_part = t.substr(0, l);
x += (double)atoi(decimal_part.c_str()) * pow(0.1, decimal_part.size());
string repeat_part = t.substr(l + 1, t.size() - decimal_part.size() - 2);
double r = pow(0.1, repeat_part.size());
x += (double)atoi(repeat_part.c_str()) * pow(0.1, decimal_part.size()) * r / (1 - r);
}
}
}
bool rational_equal(Fraction& another){
return abs(x - another.x) < e;
}
};
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;
// true
string S2 = "0.1666(6)", T2 = "0.166(66)";
cout << Solution().isRationalEqual(S2, T2) << endl;
// true
string S3 = "0.9(9)", T3 = "1.";
cout << Solution().isRationalEqual(S3, T3) << endl;
// true
return 0;
}