简单字符串处理。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctype.h> #define MAXN 25
char str[MAXN];
int ten[]; int gcd(int m, int n) {
if (m == )
return n;
if (m > n)
return gcd(n, m);
else
return gcd(m, n%m);
} int main() {
int t, a, b, ka, kb, n, m;
int i, k;
bool hascircle; ten[] = ;
for (i=; i<; ++i)
ten[i] = ten[i-]*; scanf("%d", &t);
while (t--) {
scanf("%s", str);
a = b = ;
ka = kb = ;
hascircle = false;
// ignore 0.
for (i=; str[i]; ++i) {
if (str[i] == '(') {
hascircle = true;
} else if ( isdigit(str[i]) ) {
if (hascircle) {
b = b*+str[i]-'';
kb++;
} else {
a = a*+str[i]-'';
ka++;
}
}
}
// a: integer for not circle part, b: integer for circle part
// ka: number of bits for a, kb: number of bits for b
// result = m/n;
if (kb) {
n = ten[ka+kb] - ten[ka];
m = a*ten[kb]+b-a;
} else {
n = ten[ka];
m = a;
}
k = gcd(m, n);
printf("%d/%d\n", m/k, n/k);
#ifndef ONLINE_JUDGE
fflush(stdout);
#endif
} return ;
}