Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1258 | Accepted: 379 |
Description
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
Input
Output
Sample Input
0.2... 0.20... 0.474612399... 0
Sample Output
2/9 1/5 1186531/2500000
题意:最后一位表示循环节,
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 7 #define inf 1000000000 8 #define ll long long 9 10 using namespace std; 11 char ch[10005]; 12 ll ans1,ans2; 13 ll gcd(ll a,ll b) 14 { 15 return b==0?a:gcd(b,a%b); 16 } 17 void solve(ll a,ll b,ll c,ll d) 18 { 19 ll t1=a*d+b*c,t2=b*d,t=gcd(t1,t2); 20 t1/=t;t2/=t; 21 if(t2<ans2)ans2=t2,ans1=t1; 22 } 23 int main() 24 { 25 while(~scanf("%s",ch+1)) 26 { 27 ans2=(ll)1e60; 28 int n=strlen(ch+1); 29 if(n==1)break; 30 ll b=1,a=0; 31 for(int i=3;i<=n-3;i++) 32 a=a*10+ch[i]-'0',b*=10;//三个. 33 ll t=b/10*9; 34 for(ll i=10;i<=b;i*=10,t=t+(b/i)*9) 35 solve(a/i,b/i,a%i,t); 36 printf("%lld/%lld\n",ans1,ans2); 37 } 38 }