leetcode166. Fraction to recurring Decimal

Source: Internet
Author: User

Test instructions: The numerator and denominator are given, the result is represented by a string, and the part of the loop is enclosed in "()".

Ideas:

Analog written calculation,

Initially, based on the 1/99 scenario, it is envisaged to record the interval step of the remainder with the path, but only for processing (00). 0n) where n is [1,9];

Later took the online see with Map records < remainder, appeared position > idea.

By judging whether the key in the map is repeated to determine whether the loop, through the key corresponding to the occurrence of the position, to determine the number of circular bits.

List of loggers.

Attention:

The case where the divisor is 0;

A case in which the divisor is min_int; (with long processing)

The result is a negative case;

Only a positive number of cases.

Code:

1 classSolution {2  Public:3     stringFractiontodecimal (intNumerator,intdenominator) {4         LongBei =numerator;5         LongChu =denominator;6         intFuflag =0;7         if(chu<0)8         {9Chu *=-1;TenBei *=-1; One         } A         if(bei<0) -         { -Fuflag =1; theBei *=-1; -         } -         if(bei==0) -             return "0"; +map<Long,Long>Mymap; -vector<Long>Shangvec; +         LongYup =0; A         LongFirstflag =1; at          while(bei) -         { -             if(Firstflag = =0) -Bei *=Ten; -Firstflag =0; -             LongTMP = bei/Chu; in             LongYu = bei-tmp *Chu; - Shangvec.push_back (TMP); tomap<Long,Long>:: iterator it; +it =Mymap.find (Yu); -             if(it!=mymap.end ()) the             { *                 LongXUNP = it->second; $                 stringAns ="";Panax Notoginseng                 if(Fuflag) -Ans + ="-"; the ostringstream S1; +s1<<shangvec[0]; AAns + =s1.str (); theS1.STR (""); +Ans + ="."; -                  for(LongI=1; i<=xunp;i++) $                 { $s1<<Shangvec[i]; -Ans + =s1.str (); -S1.STR (""); the                 } -Ans + ="(";Wuyi                  for(Longi=xunp+1; I<shangvec.size (); i++) the                 { -s1<<Shangvec[i]; WuAns + =s1.str (); -S1.STR (""); About                 } $Ans + =")"; -                 returnans; -             } -Mymap[yu] = yup++; ABei =Yu; +         } the         stringAns =""; -         if(Fuflag) $Ans + ="-"; the ostringstream S1; thes1<<shangvec[0]; theAns + =s1.str (); theS1.STR (""); -         if(shangvec.size () = =1) in             returnans; the  theAns + ="."; About          for(intI=1; I<shangvec.size (); i++) the         { thes1<<Shangvec[i]; theAns + =s1.str (); +S1.STR (""); -         } the         returnans;Bayi     } the};
View Code

leetcode166. 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.