A repeating decimal, also called a recurring decimal, is a number whose decimal representation eventually becomes periodic (i.e., the same sequence of digits repeats indefinitely). The repeating portion of a decimal expansion is conventionally denoted within a pair of brackets so, for example
1 / 6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66)
Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representation of 1 / 6. Given a recurring decimal representation, your task is to find an irreducible fraction that has that representation.
The first line of input contains the number of tests – T (T ≤ 100). Then T tests follow. Each test is printed in a line as a string whose length does not exceed 15. It is guaranteed to be a meaningful representation of a positive fraction.
For each test, print the result in one line in the format Case #x: a/b
4 0.125 0.(142857) 0.1(6) .2
Case #1: 1/8 Case #2: 1/7 Case #3: 1/6 Case #4: 1/5
<span style="font-size:12px;">#include <bits/stdc++.h>
using namespace std;
#define maxn 111
char a[maxn];
int n;
long long p[22];
void solve () {
n = strlen (a);
long long x = 0, y = 0, z = 0;
long long l1 = 0, l2 = 0;
for (int i = 0; i < n; i++) {
if (a[i] == '.') {
i++;
while (i < n && a[i] != '(') {
l1++;
y = y*10 + a[i]-'0';
i++;
}
}
if (a[i] == '(') {
i++;
while (i < n && a[i] != ')') {
l2++;
z = z*10 + a[i]-'0';
i++;
}
}
}
for (int i = 0; i < n && a[i] != '.'; i++) {
x = x*10 + a[i]-'0';
}
if (z == 0) {
long long pp = x*p[l1]+y, qq = p[l1];
long long g = __gcd (pp, qq);
printf ("%lld/%lld\n", pp/g, qq/g);
return ;
}
long long pp = z+(p[l2]-1)*p[l1]*x+(p[l2]-1)*y, qq = p[l1]*(p[l2]-1);
long long g = __gcd (pp, qq);
printf ("%lld/%lld\n", pp/g, qq/g);
}
int main () {
p[0] = 1;
for (int i = 1; i <= 18; i++)
p[i] = p[i-1]*10;
int t, kase = 0;
scanf ("%d", &t);
while (t--) {
scanf ("%s", a);
printf ("Case #%d: ", ++kase);
solve ();
}
return 0;
}</span>