Crossed Ladders
Input: Standard Input
Output: Standard Output
Time Limit: 1 Second

Each line of input contains three positive floating point numbers giving the values of x, y, and c.
For each line of input, output one line with a floating point number giving the width of the street in feet, with three decimal digits in the fraction.
Sample Input Output for Sample Input
30 40 10 12.619429 8.163332 3 10 10 3 10 10 1 | 26.033 7.000 8.000 9.798 |
这种解不出根的方程,二分是再好不过了,只是我对其理解不深,没有做出来,下面上代码,(时间复杂度logn)时间复杂度很重要!!!!
- #include<cstdio>
- #include<cmath>
- #include<algorithm>
- #define Min(a,b) a <= b ? a : b;
- using namespace std;
- double x, y, c;
- double get_ans(double w)
- {
- return 1 - c / sqrt(x * x - w * w) - c / sqrt(y * y - w * w);
- }
- int main()
- {
- while(~scanf("%lf%lf%lf",&x,&y,&c))
- {
- double l = 0, mid, r = Min(x,y);
- while(r - l > 1e-8)
- {
- mid = (l + r) / 2;
- if(get_ans(mid) > 0)
- l = mid;
- else
- r = mid;
- }
- printf("%.3lf\n",mid);
- }
- return 0;
- }
核心部分:
- while(r - l > 1e-8)
- {
- mid = (l + r) / 2;
- if(get_ans(mid) > 0)
- l = mid;
- else
- r = mid;
- }
现在想想,这个方法也是太精辟了,还是我没有深刻理解其内涵,,,,,加油了要。(还要说一点,二分查找之前是要排一下序的,当然,这个题目没有事先给的序列,就不用排序了)
其实,还有一个问题,就是二分排序的精度问题(感觉有1e-6和1e-8两种,其实我也分辨不清楚,其实感觉这个精度问题也不是特别要紧,就是尽量让它趋近于一个小数),关于二分,我知道的还是很少的,以后还要不断总结,二分排序啦
这里有一个链接http://blog.youkuaiyun.com/caobo1212/article/details/7905974关于二分的