Problem Description
Ray 在数学课上听老师说,任何小数都能表示成分数的形式,他开始了化了起来,很快他就完成了,但他又想到一个问题,如何把一个循环小数化成分数呢?
请你写一个程序不但可以将普通小数化成最简分数,也可以把循环小数化成最简分数。
 
Input
第一行是一个整数N,表示有多少组数据。
每组数据只有一个纯小数,也就是整数部分为0。小数的位数不超过9位,循环部分用()括起来。
 
Output
对每一个对应的小数化成最简分数后输出,占一行。
 
Sample Input
3
0.(4)
0.5
0.32(692307)
 
Sample Output
4/9
1/2
17/52
 
View Code
 1 #include <stdio.h>
 2 #include <math.h>
 3 #define MAX 20
 4 int gcd(int a, int b)//method:get the greatest divisor number
 5 {
 6     int r;
 7     while (b)
 8     {
 9           r = a % b;
10           a = b;
11           b = r;
12     }      
13     return a;
14 }    
15 int main()
16 {
17     int count1, count2, num, j, numerator, denominator, tempa, tempb, temp;
18     bool flag;
19     char c, result[MAX];
20     scanf("%d", &num);
21     while(num--)
22     {
23         //initialize the parameter
24         flag = false;
25         tempa = 0;
26         tempb = 0;
27         scanf("%s", result);
28         //handle the input, translate the string to double.
29         for (j = 2; result[j] != '\0'; j++)
30         {
31             if (result[j] == '(')
32             {
33                 count1 = j;
34                 flag = true;
35             }
36             else if (result[j] == ')')
37             {
38                 count2 = j;
39             }
40             else if (flag == false)
41             {
42                 tempa = 10 * tempa + (result[j] - '0');
43                 tempb = tempa;
44             }
45             else if (flag == true)
46             {
47                 tempb = 10 * tempb + (result[j] - '0');
48             }
49         }
50         //handle the double number respectively
51         if (flag == true)
52         {
53             denominator = pow(10.0, count2 - 3) - pow(10.0, count1 - 2);
54             numerator = tempb - tempa;
55         }
56         else
57         {
58             denominator = pow(10.0, j - 2);
59             numerator = tempa;
60         }
61         //use the method to get the greatest common divisor
62         temp = gcd(denominator, numerator);
63         //print out the result.
64         printf("%d/%d\n", numerator / temp, denominator / temp);
65     }
66     return 0;
67 }

pay attention

firstly, if you want to translate the circulate decimal to fraction. you can use the mothod as follow

A = 0.12(34)

100A = 12.(34)

10000A = 1234.(34)

10000A - 100A = 1222

then you will get the fraction.

secondly, if you can use %s, and gets, you need not use getchar().

thirdly, the complexity of this question is just classify the two aspects clearly.

for the third tip, I finished the one.

you will find a little different, one, add getchar(). two, you must add if (i > 2) continue. three, you need add the '\0'. important!!

View Code
 1 #include <stdio.h>
 2 #include <math.h>
 3 #define MAX 20
 4 int gcd(int a, int b)//method:get the greatest divisor number
 5 {
 6     int r;
 7     while (b)
 8     {
 9           r = a % b;
10           a = b;
11           b = r;
12     }      
13     return a;
14 }    
15 int main()
16 {
17     int count1, count2, num, i, j, k, numerator, denominator, tempa, tempb, temp;
18     int flag;
19     char c, result[MAX];
20     scanf("%d", &num);
21     getchar();
22     k = 0;
23     while(k < num)
24     {
25         //initialize the parameter
26         flag = 0;
27         tempa = 0;
28         tempb = 0;
29         i = 0;
30         while ((c = getchar()) != '\n')
31         {
32             if (c != ' ')
33             {
34                 result[i] = c;
35                 i++;
36             }
37         }
38         if (i < 2)
39             continue;
40         k++;
41         result[i] = '\0';
42         //handle the input, translate the string to double.
43         for (j = 2; result[j] != '\0'; j++)
44         {
45             if (result[j] == '(')
46             {
47                 count1 = j;
48                 flag = 1;
49             }
50             else if (result[j] == ')')
51             {
52                 count2 = j;
53             }
54             else if (flag == 0)
55             {
56                 tempa = 10 * tempa + (result[j] - '0');
57                 tempb = tempa;
58             }
59             else if (flag == 1)
60             {
61                 tempb = 10 * tempb + (result[j] - '0');
62             }
63         }
64         //handle the double number respectively
65         if (flag == 1)
66         {
67             denominator = pow(10.0, count2 - 3) - pow(10.0, count1 - 2);
68             numerator = tempb - tempa;
69         }
70         else
71         {
72             denominator = pow(10.0, j - 2);
73             numerator = tempa;
74         }
75         //use the method to get the greatest common divisor
76         temp = gcd(denominator, numerator);
77         //print out the result.
78         printf("%d/%d\n", numerator / temp, denominator / temp);
79     }
80     return 0;
81 }
posted on 2012-10-26 11:00  MrMission  阅读(521)  评论(0编辑  收藏  举报