Title Description:
Give the numerator and denominator of a decimal to convert the decimal to a decimal form of type string
Enclose the loop section in parentheses when a decimal appears in a loop
Topic Analysis:
The focus of this topic is to find a circulation section
For cases where the loop section is present, finding the Loop section is the focus and we
When does the loop section complete appear?
We continue to do division, each time there will be a remainder, if the current remainder has appeared before, then the circular section will appear intact
Code:
1 typedef int64_t LL;2 //typedef __int64 LL;3 4 stringITOs (LL N)//Convert shaping data to String type5 {6 if(n==0)return "0";7 stringret="";8 while(n)9 {Tenunsigned m=n%Ten; OneRet= (Char) (m+'0')+ret; An=n/Ten; - } - returnret; the } - - stringFractiontodecimal (ll numerator, ll denominator) { -Vector<ll> Quo;//Business +Vector<ll> REM;//remainder - + stringret=""; A if((numerator<0&& denominator>0) || (numerator>0&& denominator<0)) ret+='-';//determine the positive and negative sign of the final result at - //after you determine the symbol, you can convert two numbers to integer operations, but for negative (1<<31) conversions to integers it overflows, with a 64-bit integer stored - LL N; - LL m; -n=numerator<0?-Numerator:numerator; -m=denominator<0?-Denominator:denominator; in - ints=-1;//flag Cycle section start position to + -LL intpart=n/m; then=n%m; *Ret+=itos (Intpart);//Integer Part $ if(n==0)returnret;Panax Notoginseng - inttag=1;//The tag indicates whether the Loop section was found, when the tag==1 indicated that it was not found, and when tag==0 indicated that it had found a follow-up link the while(Tag && N)//when n==0 means to do + { A rem.push_back (n); then=n*Ten; + -LL quotient=n/m; $LL remainder=n%m; $ - for(intI=0; I<rem.size (); i++) - { the if(remainder==Rem[i]) - {WuyiS=i;//Cycle section start position thetag=0; - Break; Wu } - } About Quo.push_back (quotient); $n=remainder; - } - -ret+='.';//decimal point A + if(tag)//add decimals when done the { - for(intI=0; I<quo.size (); i++) $ret=ret+ (Char) (quo[i]+'0'); the } the Else //adding decimals when finding a looping section the { the for(intI=0; I<quo.size (); i++) - { in if(i==s) ret=ret+'('; theret+= (Char) (quo[i]+'0'); the } Aboutret+=')'; the } the the returnret; +}
Fraction to recurring Decimal 166