HDOJ1717 decimal score 2

Source: Internet
Author: User
Link: acm.hdu.edu. cnshowproblem. php? Pid1717 title: fractional score 2 TimeLimit: 10001000 MS (JavaOthers) MemoryLimit: 3276832768 K (JavaOthers) TotalSubmission (s): 2920 AcceptedSubmission (s): 1186ProblemDescriptionRay in Mathematics

Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1717 title: fractional score 2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s ): 2920 Accepted Submission (s): 1186 Problem Description Ray in Mathematics

Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1717

Question:

Decimal score 2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2920 Accepted Submission (s): 1186


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

30.(4)0.50.32(692307)


Sample Output

4/91/217/52


Source


Meaning: converts a decimal number into a fraction.

Solution:

Decimal places (except infinitely non-repeating decimal places) can be converted into scores. 1. convert a finite decimal number into a fraction by 0.25*100 = 25; 0.25 = 25/100 (the decimal number of 0.25 can be obtained after an approximate score); 2. convert the number of cyclically decimal points to a fraction using the following method: 0.32 (692307) * 100 = 32.692307 ①; 0.32 (692307) * 10 ^ 8 = 32692307. (692307) ②; ②-①: 0.32 (692307) * (10 ^ 8-100) = 32692275; 0.32 (692307) = 32692275/(10 ^ 8-100) (The decimal form of 0.32 (692307) can be obtained after an approximate score ).

Code:

#include 
 
  #include 
  
   int gcd(int m, int n){    int r;    while(n)    {        r = m % n;        m = n;        n = r;    }    return m;}int main(){    int t;    scanf("%d", &t);    getchar();    while(t--)    {        char s[20];        scanf("%s", s);        int len = strlen(s), index = 0;        for(int i = 0; i < len; i++)        {            if('(' == s[i])               index = i;        }        if(index)        {            int a = index - 2, c = 0, f, m = 1, n = 1;            int b = len - index - 2 + a;            for(int i = 2; i < len - 1; i++)            {                if(i == index)                {                    f = c;                    continue;                }                c = 10 * c + s[i] - '0';            }            c = c - f;            while(a--) m *= 10;            while(b--) n *= 10;            int d = n - m, e = gcd(d, c);            printf("%d/%d\n", c/e, d/e);        }        else        {            int a = 0, b = 1, m = len - 2;            for(int i = 2; i < len; i++)                a = 10 * a + s[i] - '0';            while(m--) b *= 10;            int c = gcd(a, b);            printf("%d/%d\n", a/c, b/c);        }    }    return 0;}
  
 

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.