# 2ms C++ solution share,a poor coding style

• ``````class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
ostringstream os;
//dealing with denominator == 0
if (denominator == 0){
os << INT_MAX;
return os.str();
}
//dealing with numerator == 0
if(numerator==0)
return "0";
//others
if ((numerator<0) ^ (denominator < 0))
os << '-';
long long x = abs((long long)numerator), y = abs((long long)denominator);
os << x / y;
if ((x %= y) == 0)
return os.str();
os << '.';

long long ax = x, ay = y;
int i = 0, j = 0;
//find the count of non_repeating part,it depends on how many 2 or 5 factor
// are there in denominator
while (y % 2 == 0){
y /= 2;
++i;
}
while (y % 5 == 0){
y /= 5;
++j;
}
int k = max(i, j);
//non_repeating part
while (k != 0 && ax != 0){
ax = ax * 10;
os << ax / ay;
ax %= ay;
--k;
}
if (ax == 0)
return os.str();
long long m = ax;
os << '(';
//repeating part
//to find the repeating part ,we need to find when the denominator(here it is ax) repeats,
//then we stop;
while (true){
ax = ax * 10;
os << ax / ay;
if ((ax %= ay) == m)
break;
}
os << ')';
return os.str();
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.