链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1717
举例:
0.24333333…………=(243-24)/900=73/300
0.9545454…………=(954-9)/990=945/990=21/22
代码:
#include<stdio.h> #include<string.h> #define N 110 int gcd(int a, int b) { return b==0?a:gcd(b, a%b); } int main() { int t; scanf("%d", &t); while(t--) { char s[N]; scanf("%s", s); int a=0, b=0, c=1, d=1, e=1, z, flag=0; for( z=0; s[z]; z++) if(s[z]=='.') break; for(int i=z+1; s[i]; i++) { if(s[i]=='(') { flag=1; for(int j=i+1; s[j]; j++) { if(s[j]==')') break; b = b*10 + s[j]-'0'; d *= 10; e *= 10; } break; } a = a*10 + s[i]-'0'; c *= 10; d *= 10; } if(flag) { a = a*e + b - a; d = d-c; } int g = gcd(a, d); printf("%d/%d\n", a/g, d/g); } return 0; }
勿忘初心