decimal fraction to Infinite loop

Source: Internet
Author: User

As we all know, the finite fraction is another form of the decimal score, so any finite fraction can be directly written into a few, a few percent, a few thousand ... The number. So can the infinite number of fractions be converted into fractions?

First we want to make it clear that infinite decimals can be divided into two categories according to whether the fractional part loops: Infinite loop decimals and infinite non-cyclic decimals. Infinite not repeating decimal the score, which will be explained in detail in the middle school, and the fractional number of infinite loops can be converted into fractions. So, how does an infinite loop decimal number turn into fractions? Because its fractional part is infinite, it is obviously impossible to write a very few, a few percent, a few thousand ... The number. In fact, it is difficult to repeating decimal fractions in an infinite number of decimal digits. So I'm going to start here and find a way to "cut off" the "big tail" of an infinite loop of decimals. The strategy is to enlarge the infinite loop by 10 times times, 100 times times, or 1000 times times with the multiplication method. Make the enlarged infinite loop decimal with the original infinite loop decimal "big tail" exactly the same, and then subtract the two numbers, "big tail" is not cut off! Let's take a look at two examples:

⑴ the 0.4747 ... and 0.33 ... Into fractions.

Want to 1:0.4747......x100=47.4747 ...

0.4747......x100-0.4747......=47.4747 ... -0.4747 ...

(100-1) x0.4747......=47

That is 99x0.4747 ... =47

So 0.4747......=47/99



Want to 2:0.33......x10=3.33 ...

0.33......x10-0.33......=3.33 ... -0.33 ...

(10-1) x0.33......=3

namely 9x0.33......=3

So 0.33......=3/9=1/3

Thus, the pure repeating decimal fraction, its fractional part can be written such a fraction: pure repeating decimal of the minimum number of cycles is a few, the denominator is composed of several 9 of the number, the molecule is a pure repeating decimal in a circular section of the number.

⑵ the 0.4777 ... and 0.325656 ... Into fractions.

Want to 1:0.4777......x10=4.777 ... ①

0.4777......x100=47.77 ... Ii

Use ②-① to get:

0.4777......x90=47-4

So, 0.4777......=43/90



Want to 2:0.325656......x100=32.5656 ... ①

0.325656......x10000=3256.56 ... Ii

Use ②-① to get:

0.325656......x9900=3256.5656 ... -32.5656 ...

0.325656......x9900=3256-32

So, 0.325656......=3224/9900

The pure repeating decimal is rewritten into fractions, and the numerator is a number consisting of a cyclic section; The denominator numbers are the same number of 9,9 as the number in the Loop section.

The mixed repeating decimal is rewritten into fractions, the numerator is the number of numbers that are not part of the loop and the number of the first loop, minus the number of numbers that are not part of the loop; The first number of the denominator is 9, the last number is the number of 0,9 is the same as that of the loop section, and the number of 0 is the same as the

Attach the Great God code:

#include <iostream>
using namespaceStd;
intF(intU,intV)
{

while(U%V)
{

intW=U%V;
U=V;
V=W;
}

returnV;
}

int main()
{

intT;
while(Cin>>T)
{

while(T--)
{

CharA[15];
Cin>>A;
intP=0,Q=0,T=0,X=0,Y,K=1,L=1,Max;
for(intI=2;A[I]!=' + ';I++)
{

if(!T &&A[I]!=‘(‘) {P++;X*=10;X+=A[I]-' 0 ';}
if(T &&A[I]!=‘)‘) {Q++;Y*=10;Y+=A[I]-' 0 ';}
if(A[I]==‘(‘) {T=1;Y=X;Q=P;}
}

if(!Q)
{

while(P--)
K*=10;
Max=F(X,K);
X/=Max;
K/=Max;
cout<<X<<‘/‘<<K<<Endl;
}

Else
           {
intM=Y-X;
while(P--)
K*=10;
while(Q--)
L*=10;
intN=L-K;
Max=F(M,N);
M/=Max;
N/=Max;
cout<<M<<‘/‘<<N<<Endl;
}
}
}

return0;
}

decimal fraction to Infinite loop

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.