-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.cpp
More file actions
41 lines (41 loc) · 866 Bytes
/
Fraction.cpp
File metadata and controls
41 lines (41 loc) · 866 Bytes
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
string Solution::fractionToDecimal(int A, int B) {
long long int a = A;
long long int b = B;
int sign = 1;
string s = "";
if((a<0 && b>0) || (a>0 && b<0)){
s += "-";
}
a = abs(a);
b = abs(b);
// cout<<A<<B;
if(a==0)
return "0";
long long int division = a/b;
s += to_string(division);
a %= b;
if(a==0)
return s;
s += ".";
bool repeat = false;
int val;
unordered_map<long long int,long long int> hash;
while(a!=0 && !repeat){
a*= 10;
if(hash.find(a)==hash.end())
hash.insert({a,s.size()});
else{
repeat = true;
val = hash[a];
break;
}
int t = a/b;
s += to_string(t);
a %= b;
}
if(repeat){
s += ")";
s.insert(val,"(");
}
return s;
}