-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction_to_Recurring_Decimal.cpp
More file actions
37 lines (37 loc) · 1.05 KB
/
Fraction_to_Recurring_Decimal.cpp
File metadata and controls
37 lines (37 loc) · 1.05 KB
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
//string fractionToDecimal(int numerator, int denominator)
//思路是用unordered_map来记录出现的数,如果重复出现,则是循环
string fractionToDecimal(long long numerator, long long denominator)
{
string result;
if (numerator == 0)
return "0";
if (denominator == 0)
return "";
//异号
if ((numerator < 0) ^ (denominator < 0))
result += "-";
long long a = abs((long long)numerator);
long long b = abs((long long)denominator);
//求得整数部分
result += to_string(abs(a) / abs(b));
if (numerator%denominator == 0)
return result;
result += ".";
//求小数部分
unordered_map<int, int> map;
for (long long r = a%b; r; r %= b)
{
if (map.count(r) > 0)
{
//在数r位置的前面插入'('
result.insert(map[r], 1, '(');
result += ")";
break;
}
//保存数r出现的位置
map[r] = result.size();
r *= 10;
result += to_string(r / b);
}
return result;
}