Description
Input
Output
Sample Input
Sample Output
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<vector> 7 #define M(a,b) memset(a,b,sizeof(a)) 8 #include<map> 9 #include<stdlib.h> 10 #define eps 1e-5 11 using namespace std; 12 13 double x,y; 14 15 int main() 16 { 17 while(scanf("%lf%lf",&x,&y)==2) 18 { 19 double lim = (y+1)/x-eps; 20 double pri = 1; 21 int num = 1; 22 int tim = 0; 23 double all = 1; 24 if(x>y) {puts("-1"); continue;} 25 while(1) 26 { 27 if(all-y>0||fabs(all-y)<eps) 28 break; 29 //cout<<lim<<endl; 30 int tmp = (int)((lim - pri)/(1.0/num)); 31 pri+=tmp*(1.0/num); 32 //cout<<pri<<‘ ‘<<tmp<<endl; 33 tim+=tmp; 34 all+= tmp; 35 if(tmp==0) 36 { 37 int tm = (int)(1.0/(lim-pri)-num)+1; 38 if(tm>(y+1-eps-all)/pri) 39 tm = (int)((y+1-eps-all)/pri); 40 num+=tm; 41 tim+=tm; 42 all+=pri*tm; 43 } 44 // cout<<tim<<endl; 45 //cout<<all<<endl; 46 } 47 printf("%d\n",tim); 48 } 49 return 0; 50 }
原文:http://www.cnblogs.com/haohaooo/p/4018051.html