Crossed ladders
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5012 | Accepted: 1921 |
Description
A narrow street is lined with tall buildings. An x foot long ladder is rested at the base of the building on the right side of the street and leans on the building on the left side. A y foot long ladder is rested at the base of the building on the left side of the street and leans on the building on the right side. The point where the two ladders cross is exactly c feet from the ground. How wide is the street?
![](https://i-blog.csdnimg.cn/blog_migrate/23a69e311553916bf153015cc1b4e905.jpeg)
Input
Each line of input contains three positive floating point numbers giving the values of x, y, and c.
Output
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
30 40 10 12.619429 8.163332 3 10 10 3 10 10 1
Sample Output
26.033 7.000 8.000 9.798
就是求俩栋楼之间的距离,用二分算法将底边二分,注意(在地边中,枚举的mid越大,d的取值应该比他大,因为mid大,a1,a2小,tan小,那么d就大了)。
代码如下:
<pre name="code" class="cpp">#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <list> #include <map> #include <cmath> #include <stack> #include <vector> #include <cmath> #include <queue> using namespace std; const double eps=1e-10; double x,y,c; int main() { int t; while(~scanf("%lf%lf%lf",&x,&y,&c)) { double l=0,r=min(x,y); int Size=100; while(Size--) { double mid=(l+r)/2.0; double a1=acos(mid/x),a2=acos(mid/y); double d=c/tan(a1)+c/tan(a2); if(d-mid<eps){ l=mid+eps; } else{ r=mid-eps; } } printf("%.3lf\n",l); } return 0; }