题意:
很有意思的一道题,,将一个无限循环小数转化成分母最小的精确分数值....,循环的部分不一定是最后一位。
Sample Input
View Code
0.2...
0.20...
0.474612399...
0
Sample Output
2/9
1/5
1186531/2500000
1 #include<set> 2 #include<map> 3 #include<queue> 4 #include<stack> 5 #include<cmath> 6 #include<cstdio> 7 #include<cstring> 8 #include<iostream> 9 #include<algorithm> 10 #define RG register int 11 #define rep(i,a,b) for(RG i=a;i<=b;++i) 12 #define per(i,a,b) for(RG i=a;i>=b;--i) 13 #define ll long long 14 #define inf (1ll<<60) 15 using namespace std; 16 ll n,mx,mn,len; 17 char s[20]; 18 inline int read() 19 { 20 int x=0,f=1;char c=getchar(); 21 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 22 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 23 return x*f; 24 } 25 26 ll gcd(ll a,ll b){ return (a%b==0)?b:gcd(b,a%b);} 27 28 void work() 29 { 30 rep(i,2,len+1) 31 { 32 ll A=n,B=0,C=i-2;ll K=len-C; 33 rep(j,2,i-1) B=B*10+s[j]-'0'; 34 ll X=A-B,Y=pow(10,C)*(pow(10,K)-1ll); 35 ll g=gcd(X,Y); 36 if(Y/g<mn) mx=X/g,mn=Y/g; 37 } 38 printf("%lld/%lld ",mx,mn); 39 } 40 41 int main() 42 { 43 while(1) 44 { 45 scanf("%s",s);n=len=0,mn=inf; 46 if(strlen(s)==1) break; 47 for(RG i=2;;i++) 48 { 49 if(s[i]=='.') break; 50 n=n*10+s[i]-'0',len++; 51 } 52 work(); 53 } 54 return 0; 55 }