Fraction to recurring Decimal

Source: Internet
Author: User

Given integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part was repeating, enclose the 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) ".

Analysis: 1190000003794677

The complexity of hash table method

Time O (n) space O (n)

Ideas

The integer part is good to deal with, as long as the attention to the sign of the distinction is OK, but how to deal with the fractional part of it. If it's just a simple division, we multiply the remainder by 10, then divide by dividend to get the decimal of the current bit, and get a new remainder until the remainder is 0. The difficulty is that for endless repeating decimal, we always do this and never let the remainder become 0. Here we can use a hash table to record the remainder of each time, if the remainder is repeated, the description will produce a loop. In order to find the part of the loop in the decimal, when we use the hash table, we also have to record the number of decimal places for each remainder, so that once we encounter repetition, we know where to start the loop.

Attention

If the input dividend is large, then the remainder multiplied by 10 May overflow, so we use long to save numerator and denominator.

1  PublicString Fractiontodecimal (intNumerator,intdenominator) {2             Longnum =numerator;3             LongDen =denominator;4             if(num = =0|| Den = =0){5                 return "0";6             }7             //Judging result sign8Boolean negative = (num >0&& den <0) || (Num <0&& den >0);9num =math.abs (num);TenDen =Math.Abs (DEN); One             //get the integer part AString Integ = (negative?"-":"") + string.valueof (num/den); -             //If there are a number of decimal parts -             if(num% den! =0){ thenum = num%den; -Hashmap<long, integer> map =NewHashmap<long, integer>(); -                 intpos =0; - map.put (num, POS); +StringBuilder Frac =NewStringBuilder (); -                 //calculate the number of decimal parts +                  while(num! =0){ A                     //first the calculated decimal number Plus, and then determine whether the remainder is repeated, if the remainder is repeated, the decimal will be repeated from the next start atnum = num *Ten; -Frac.append (num/den); -num = num%den; -                     //If the remainder appears before, it indicates that there is a loop, where the last occurrence is the current position is the part of the Loop -                     if(Map.containskey (num)) { -                         //separating the non-circular and cyclic parts inString pre = frac.substring (0, map.Get(num)); -String loop = frac.substring (map.Get(num)); to                         //returns the result with a loop +                         returnInteg +"."+ Pre +"("+ Loop +")"; -                     } thepos++; *                     //record the current remainder and the position of his corresponding decimal $ map.put (num, POS);Panax Notoginseng                 } -                 //returns the result of a decimal with no loops the                 returnInteg +"."+frac.tostring (); +             } A             //returns the result without decimals the             returnInteg; +}

Fraction to recurring Decimal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.