二分搜索 HDOJ 2675 Equation Again

 

题目传送门

 1 /*  2  二分搜索:式子两边取对数,将x提出来,那么另一边就是一个常数了,函数是:lnx/x。二分搜索x,注意要两次  3 */  4 #include <cstdio>  5 #include <algorithm>  6 #include <cmath>  7 using namespace std;  8  9 const double e = exp (1.0); 10 11 double cal(double x) { 12 return log (x) / x; 13 } 14 15 int main(void) { //HDOJ 2675 Equation Again 16 //freopen ("HDOJ_2675.in", "r", stdin); 17 18 double y, k; 19 while (scanf ("%lf", &y) == 1) { 20 k = (1 + log (y)) / (e * y); 21 //if (k > 1 / e) { //囧! 22 // puts ("Happy to Women’s day!"); continue; 23 //} 24 if (k == 1 / e) { 25 printf ("%.5f\n", e); continue; 26  } 27 28 double x1, x2; 29 double l = 0, r = e; 30 for (int i=1; i<=100; ++i) { 31 double mid = (l + r) / 2; 32 if (cal (mid) >= k) r = mid; 33 else l = mid; 34  } 35 x1 = l; 36 l = e, r = 1e9; 37 for (int i=1; i<=100; ++i) { 38 double mid = (l + r) / 2; 39 if (cal (mid) >= k) l = mid; 40 else r = mid; 41  } 42 x2 = l; 43 printf ("%.5f %.5f\n", x1, x2); 44  } 45 46 return 0; 47 }

 

你可能感兴趣的:(IO)