(Hdu step 2.1.8) Fractional fraction 2 (fractional fraction-including the repeating decimal fraction)

Source: Internet
Author: User

Label:

Topic:

 Decimal fraction of 2 Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total submission (s): 2783 Accepted Submission (s): 993 Problem Descriptionray in math class to listen to the teacher said, any decimal can be expressed in the form of components, he began to turn up, and soon he finished, but he thought of a question, how to put a repeating decimal into fractions?Please write a program not only can the ordinary decimal into the simplest fraction, can also be repeating decimal into the simplest fraction. The first line of input is an integer n, which indicates how many sets of data.Each group of data has only one decimal fraction, that is, the whole number is divided into 0. The number of decimal places does not exceed 9 bits, and the loop section is enclosed in (). Output has a single row for each of the corresponding decimal fractions. Sample Input`30. (4) 0.50.32 (692307)` Sample Output`4/91/217/52` Source2007 Provincial Training Team Practice (2) Recommendlcy

Topic Analysis:

Simple question. Fractional fraction. Where the "decimals" includes the cyclic decimals. Repeating decimal can be divided into the following two types:

1) Pure loop decimal. Pure repeating decimal have no non-cyclic sections, all of which consist of a cyclic section. It has the following rules when it is converted to fractions:

Numerator = number on a cyclic section

Denominator =9 ..... (The number of 9 depends on the number of bits in the loop section)

2) Non-pure cyclic decimals.

Numerator = Non-cyclic part * The cyclic section needs to enlarge the multiplier + the number on the cyclic section-the non-cyclic part;

Denominator =999...000 ... (9 of the number depends on the number of bits in the Loop section, and the number of 0 depends on the number of bits in the non-circular section)

1.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 ... ②

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

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

2.(1) Pure repeating decimal (only refers to the whole number of parts divided into 0) into fractions, the denominator of the fraction is composed of 9, 9 is equal to the number of bits of a circular section, the molecule is composed of a circular section of the figures.
such as: 0,234234234....=234/999
0.111111=1/9

(2) when the non-pure repeating decimal into fractions, the denominator consists of 9 and 0, where 9 is equal to the number of bits of a cyclic section, and 0 is equal to the number of bits in the non-cyclic part. A molecule is a number minus the non-cyclic part of the first iteration of the first circular section.
such as: 0,76345345345 .... = (76345-76)/99900
0.0243434343.........= (243-2)/9900
0.811111 ..... = (81-8)/90=73/90

The code is as follows:

`/* * e2.cpp * * Created on:2015 February 23 * author:administrator * * #include <iostream> #include <cstdio>usi ng namespace std;/** * Ask greatest common divisor */int gcd (int a,int b) {int Temp;while (b! = 0) {temp = B;b = A%b;a = temp;} return A;} /** * Convert decimals to corresponding simplest fractions * str: decimal string */void Xiaoshu2fenshu (string str) {int S1 = 0;//fractional non-circular part int p1 = 1;//fractional non-circular part enlarged to integer required multiplier  int s2 = 0;//fractional loop part int p2 = 1;//enlarges the loop portion of the decimal to an integer that needs to be enlarged by a multiple of int length = Str.length (); int i = 0;while (str[i]! = '. ' && I < length) {//Find the starting position of the non-cyclic part of the decimal i++;} I++;while (str[i]! = ' (' && i < length) {//Get the non-circular portion of the decimal s1 = s1*10 + str[i]-' 0 ';p 1 *= 10;i++;} I++;while (str[i]! = ') ' && i < length) {//Get the loop portion of the decimal s2 = s2*10 + str[i]-' 0 ';p 2 *= 10;i++;} int t1;//numerator int t2;//denominator int t;//numerator and denominator greatest common divisor for the simplest fraction if (s2! = 0) {//If the loop portion is not 0, then the decimal is repeating decimal T1 = s1*p2+s2-s1;// Calculate repeating decimal t2 = (p2-1) *p1;//calculates the denominator}else{//otherwise weight normal fraction t1 = s1;//The numerator of the ordinary fraction is the non-cyclic part t2 = p1;//The denominator of the ordinary decimal is the multiple of the fractional loop that needs to be enlarged}t = GCD (T1,T2);//greatest common divisor of numerator and denominator. For the simplest score cout << t1/t << "/"<< t2/t << endl;//string result = t1/t +"/"+ t2/t;//do not award integer type and string type directly splicing, so stitching is wrong//string result = itoa (t 1/T) + "/" + itoa (t2/t);//return result;} int main () {int t;scanf ("%d", &t), while (t--) {string str;cin >> Str;xiaoshu2fenshu (str);//cout << Xunhuanxiaoshu2fnenhu (str) << Endl;} return 0;}`

(Hdu step 2.1.8) Fractional fraction 2 (fractional fraction-including the repeating decimal fraction)

Related Keywords:
Related Article