-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathEqual Rational Numbers.cpp
59 lines (49 loc) · 1.43 KB
/
Equal Rational Numbers.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
// Runtime: 3 ms (Top 30.23%) | Memory: 5.8 MB (Top 100.00%)
class Solution {
public:
double toDouble(string s){
// Strings for each integral, fractional, and repeating part
string in="", fn="", rn="";
int i=0;
// Integral
while(i<s.size() && s[i]!='.'){ in+=s[i]; i++; }
// Fractional
i++;
while(i<s.size() && s[i]!='('){ fn+=s[i]; i++; }
// Repeating
i++;
while(i<s.size() && s[i]!=')'){ rn+=s[i]; i++; }
// Number
double a = 0;
// Adding integral part
if(!in.empty()) a=stoi(in);
i=0;
// Adding fractional part
while(i<fn.size()){
a = a*10 + fn[i] - '0';
i++;
}
// Adding repeating part
if(i < 8){
// If repeatig part isn't there then just add 0s
if(rn.size() == 0){
while(i <= 8){ a*=10;i++; }
}
else {
int j=0;
while(i <= 8){
a = a*10 + rn[j%rn.size()] - '0';
j++;
i++;
}
}
}
// Return number/10^8
return a/10e8;
}
bool isRationalEqual(string s, string t) {
// Find absolute differance till 8 digits after decimal and compar if its lesser
double ans = abs(toDouble(s) - toDouble(t));
return ans < 0.000000002;
}
};