uva 10566 Crossed Ladders (二分)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1507
比较简单的一题,直接对答案二分。因为对于同一组case,答案m越大,交点的高度就越小,可以从计算交点的函数中看出来。计算交点,假设mx=sqrt(sqr(x)-sqr(m)),my=sqrt(sqr(y)-sqr(m)),这两个是梯子跟两堵墙的交点。那么,交点的高度就是mx*my/(mx+my)了。
代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 7 using namespace std; 8 9 const double EPS = 1e-8; 10 double x, y, c; 11 12 template<class T> T sqr(T x) { return x * x;} 13 double cal(double m) { 14 double xm = sqrt(sqr(x) - sqr(m)); 15 double ym = sqrt(sqr(y) - sqr(m)); 16 //cout << m << ' ' << xm * ym / (xm + ym) << endl; 17 return xm * ym / (xm + ym); 18 } 19 20 double DC2() { 21 double l = 0.0, r = min(x, y), m; 22 while (r - l > EPS) { 23 double m = (l + r) / 2.0; 24 if (cal(m) < c) r = m; 25 else l = m; 26 } 27 return l; 28 } 29 30 int main() { 31 while (cin >> x >> y >> c) { 32 printf("%.3f\n", DC2()); 33 } 34 return 0; 35 }
——written by Lyon