HDU 1717 decimal score 2

Source: Internet
Author: User

Hduoj mathematics

Decimal score 2 Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 3040 accepted submission (s): 1239

Problem description

Ray listened to the teacher in the mathematics class and said that any decimal number can be expressed as a score. He started to convert it and soon completed, but he thought of another problem, how can we convert a repeating decimal point into a fraction?
Please write a program that not only converts normal decimals into the simplest scores, but also converts cyclic decimals into the simplest scores.


Input

The first row is an integer N, indicating the number of groups of data.
Each group of data has only one pure decimal number, that is, the integer is 0. The number of decimal places cannot exceed 9 digits, and the loop part is enclosed.


Output

Converts each corresponding decimal number into the simplest score and then outputs the result, occupying a row.


Sample Input

3

0. (4)

0.5

0.32 (692307)


Sample output

4/9

1/2

17/52



Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1717


Question Analysis: divide the number into two parts: the cyclic part and the non-cyclic part. The final answer is the sum of the two parts. Set the number of the non-cyclic part to num1, indicates that the score result is num1/len1 (len1 is the base of the number of digits plus 1 in num1). For example, 0.32 indicates 32/100, and the sum of the proportional series is used in the loop part. Set the number in the loop to num2 and the length to len2 (len2 is the number of digits in num2 plus the base number of 1). We find that from the decimal point, k is the number of digits in the base number) the bitwise start loop part forms an proportional sequence. Its first entry is num2/len2 and its public ratio is 1/len2. According to the summation formula of the proportional sequence Sn = A1 (1-Q ^ N)/(1-Q ), here Q is 1/len2 obviously len2> = 10, so when n tends to be infinite (1/len2) ^ n tends to be 0, so Sn = A1/(1-Q) = num2/(len2-1) Note that the Sn is counted from the K digit after the decimal point, so the SN must be divided by len1.

If you want to add a/B + C/D = (AD + BC)/BD to the two scores, the score must be (num1/len1) + num2/(len2-1) * len1) after simplification

(Num1 * (len2-1) + num2)/(len1 * (len2-1). Finally, the greatest score of the denominator is the answer. Here we only talk about the case of cyclic decimal places. If there is no circular decimal places, we can directly obtain num1/len1 and re-divide it.


// For the idea, see the code. The program implementation is simple. # include <cstdio> # include <cstring> int gcd (int A, int B) {return B = 0? A: gcd (B, A % B);} int main () {int T, Len, flag; // nt1 non-cyclic part of the molecule dt1 non-cyclic part of the denominator // nt2 cyclic part of the molecule, dt2 cyclic part of the denominator int nt1, nt2, dt1, dt2; char STR [50]; scanf ("% d", & T); While (t --) {flag = 1; nt1 = nt2 = 0; dt1 = dt2 = 1; scanf ("% s ", str); Len = strlen (STR); For (INT I = 2; I <Len; I ++) {If (STR [I] = '(') {flag = 0; continue;} If (STR [I]> = '0' & STR [I] <= '9') {If (FLAG) {dt1 * = 10; nt1 = nt1 * 10 + STR [I]-'0';} else {dt2 * = 10; nt2 = nt2 * 10 + STR [I]-'0' ;}} if (FLAG) printf ("% d/% d \ n", nt1/gcd (nt1, dt1), dt1/gcd (nt1, dt1); else {int temp = gcd (nt1 * (dt2-1) + nt2, dt1 * (dt2-1 )); printf ("% d/% d \ n", (nt1 * (dt2-1) + nt2)/temp, dt1 * (dt2-1)/temp );}}}





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.