java - Fraction to Recurring Decimal -


working on problem, , did few reference similar solutions. 1 thing confuse is, why break loop long there 1 repetitive number? possible number repeat 2-3 times , changed different number? thanks.

i mean part specifically,

        if (map.containskey(num)) {             int index = map.get(num);             res.insert(index, "(");             res.append(")");             break;         } 

the problem,

given 2 integers representing numerator , denominator of fraction, return fraction in string format.

if fractional part repeating, enclose repeating part in parentheses.

for example,

given numerator = 1, denominator = 2, return "0.5". given numerator = 2, denominator = 1, return "2". given numerator = 2, denominator = 3, return "0.(6)".

public class solution {     public 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();     } } 

thanks in advance, lin

the code doesn't stop when sees digit repeated. stops when notes has reached state in. if reaches same state again, means repeat division have done, means dividend , remainder going same, , going same series of steps have done.

when happens, means repetition, , stops , adds parentheses.

for example, let's divide 123 999. should give repeating decimal 0.123123123..., output should 0.(123).

  • 123 / 999 0. remainder 123. start 0.
  • multiply remainder 10. have 1230 / 999. dividend 1, remainder 231. have 0.1
  • multiply remainder 10. have 2310 / 999. dividend 2, remainder 312. have 0.12
  • multiply remainder 10. have 3120 / 999. dividend 3, remainder 123. have 0.123
  • multiply remainder 10. have 1230 / 999... wait, have done that! means continue divide, we'll number again , again. stop , put parentheses around repeating part.

the map there tell numbers have divided, , @ index in stringbuilder. when find number have divided, use index know insert parenthesis.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -