力扣
class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) return "0";
StringBuilder fraction = new StringBuilder();
if ((numerator < 0) ^ (denominator < 0)) {
fraction.append("-");
}
long divided = Math.abs(Long.valueOf(numerator));
long divisor = Math.abs(Long.valueOf(denominator));
fraction.append(String.valueOf(divided / divisor));
long remainer = divided % divisor;
if (remainer == 0) return fraction.toString();
fraction.append(".");
Map<Long, Integer> map = new HashMap<>();
while (remainer != 0) {
if (map.containsKey(remainer)) {
fraction.insert(map.get(remainer), "(");
fraction.append(")");
break;
}
map.put(remainer, fraction.length());
remainer *= 10;
fraction.append(remainer / divisor);
remainer %= divisor;
}
return fraction.toString();
}
}
力扣
class Solution {
public int[] constructRectangle(int area) {
int w = (int) Math.sqrt(area);
while (area % w != 0) {
w--;
}
return new int[] {area / w, w};
}
}