Decimal score 2
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2206 accepted submission (s): 892
Problem descriptionray was told by the teacher in the mathematics class 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 Program Not only can ordinary decimals be converted into the simplest fraction, but also can be converted into the simplest fraction by repeating decimals.
The first line of input 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
Source2007 provincial training team exercises (2)
Recommendlcy
Solution: Read the information of common decimal places and circular decimal places, and simply write the information according to the formula of number theory. The test data of the question is not drilled into data (such as 0.0 ).
Mathematical formula:
N indicates the number of common decimal places, and X indicates the ordinary decimal places (converted to an integer)
M indicates the number of decimal places in the loop, and y indicates the number of decimal places in the loop (converted to an integer)
Decimal number can be expressed as 0.x( y)
The original score is(X * (10 ^ m-1) + Y)/(10 ^ N * (10 ^ s-1 ))
You only need to obtain the maximum approximate number of the denominator, and then divide the denominator by the approximate number to get the simplest score, that is, the answer to the question.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int main () {int B [10]; int I; int X, Y; int n, m; int t; char a [20]; B [0] = 1; for (I = 1; I <10; I ++) // denominator auxiliary array build B [I] = B [I-1] * 10; scanf ("% d", & T); While (t --) {scanf ("% s", a); int STR = strlen (a); for (I = 0; I <STR; I ++) if (A [I] = '(') break; n = I-2; // the digits M = str-i-2; // number of digits in the fractional part of the loop if (n> 0 & M <= 0) // only digits in the ordinary decimal part, and digits in the loop {sscanf (a + 2, "% d ", & X); y = B [N];} else if (M> 0 & n <= 0) // only contains the fractional part of the loop, there are no common decimals {sscanf (a + 3, "% d", & X); y = B [m]-1 ;}else // There is a circular decimal part, there is a normal decimal {sscanf (a, "0.% d (% d) ", & X, & Y); X = x * (B [m]-1) + Y; y = B [N] * (B [m]-1);} Int J, K = 1; I = x; j = y; while (k) // calculate the maximum common approx. It is used to simplify the score {if (I <j) Swap (I, j); k = I % J; I = J; j = K ;} printf ("% d/% d \ n", X/I, Y/I);} return 0 ;}