题意:两栋楼之间有两个梯子,如图中的虚线所示,一个梯子的长度为x,另一个梯子的长度为y,两个梯子的交点离地面的高度为c,问两栋楼之间的距离。
看到这类的几何题,配有几张情景图,总是有一种莫名的亲切感,有一种想秒A的冲动>=<
解题思路:
在纸上画出图,设宽度为w,交点距左楼距离为a,则根据三角形相似可以推出:
将第二个方程带入到第一个,化简得到: 1=c/sqrt(x*x-w*w)+c/sqrt(y*y-w*w);将得到方程二分求解,即可得到题目所求
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-6;//注意eps定义成double 型,否则会超时
double x,y,c;
double fun(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 left=0,mid,right=x;
while(right-left>eps)
{
mid=(left+right)/2;
if(fun(mid)>0)
left=mid;
else right=mid;
}
printf("%.3lf\n",mid);
}
return 0;
}