题目描述
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?
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
Source
The UofA Local 2000.10.14
算法1
浮点数二分
参考文献
http://poj.org/problem?id=2507
分析过程
C++ 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
double x, y, c;
bool check(double mid)
{
double ry = sqrt(y * y - mid * mid);
double rx = sqrt(x * x - mid * mid);
double h = rx * ry / (rx + ry);
if (h < c) return true; //h<c,间距取大了,会让梯子下滑,这样交点高度就小了
else return false;
}
int main()
{
scanf("%lf%lf%lf", &x, &y, &c);
double l, r;
if (x < y) l = 0, r = x; //取小的边长,作为右边界,因为梯子不能趴地上吧
else l = 0, r = y;
while (r - l > 1e-5) //答案保留小数点后3位,这里多取2位即可 POJ原题数据1e-4也能过
{
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
printf("%.3lf\n", l);
return 0;
}
这思路,太厉害了!
老师可以可以 思路很清晰 还是我的公式没有推对 数学还是差呀
加油,你会更厉害!
老师真厉害
你会更厉害的
%%%
老哥你的文章很好.这个负数赞的原因是在初始界面点了爱心然后我删了结果又点的时候就变成负数了.我也是刚发现这个bug,对你造成的影响表示抱歉
收到。这个BUG我很早前也发现了,这个不是事,y总知道的,不要紧的BUG。嗯,其实,这个可以无限负分刷点赞,有兴趣你可以玩玩…
2333我早就有-18个了
hhhhh