You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
2 years ago
|
package class32;
|
||
|
|
||
|
import java.util.HashMap;
|
||
|
|
||
|
public class Problem_0166_FractionToRecurringDecimal {
|
||
|
|
||
|
public static String fractionToDecimal(int numerator, int denominator) {
|
||
|
if (numerator == 0) {
|
||
|
return "0";
|
||
|
}
|
||
|
StringBuilder res = new StringBuilder();
|
||
|
// "+" or "-"
|
||
|
res.append(((numerator > 0) ^ (denominator > 0)) ? "-" : "");
|
||
|
long num = Math.abs((long) numerator);
|
||
|
long den = Math.abs((long) denominator);
|
||
|
// integral part
|
||
|
res.append(num / den);
|
||
|
num %= den;
|
||
|
if (num == 0) {
|
||
|
return res.toString();
|
||
|
}
|
||
|
// fractional part
|
||
|
res.append(".");
|
||
|
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
|
||
|
map.put(num, res.length());
|
||
|
while (num != 0) {
|
||
|
num *= 10;
|
||
|
res.append(num / den);
|
||
|
num %= den;
|
||
|
if (map.containsKey(num)) {
|
||
|
int index = map.get(num);
|
||
|
res.insert(index, "(");
|
||
|
res.append(")");
|
||
|
break;
|
||
|
} else {
|
||
|
map.put(num, res.length());
|
||
|
}
|
||
|
}
|
||
|
return res.toString();
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
System.out.println(fractionToDecimal(127, 999));
|
||
|
}
|
||
|
|
||
|
}
|