poj 2507 Crossed ladders

来源:互联网 发布:齐鲁软件大赛2016 编辑:程序博客网 时间:2024/06/11 11:23
Crossed ladders
Time Limit: 1000MS Memory Limit: 65536KTotal 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? 

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 1012.619429 8.163332 310 10 310 10 1

Sample Output

26.0337.0008.0009.798
题解:设要求的边为w,交点到左边墙的距离为a,则根据三角形相似得   (w-a)/w = c/sqrt(y*y-w*w)......(1)
a/w = c/sqrt(x*x-w*w)......(2)   由(1)(2)式得到 c/sqrt(y*y-w*w)+c/sqrt(x*x-w*w) = 1 。利用二分法求w的值,注意精确度。。。。代码如下
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#define eps 1e-10using namespace std;int main(){    double x, y, c;    while(~scanf("%lf%lf%lf",&x,&y,&c))    {        double l = 0.0 , r = x < y ? x : y;        int size = 100;        while(size--)        {            double mid = (l + r) / 2;            if( c / sqrt(x * x - mid * mid) + c / sqrt(y * y - mid * mid) <= 1)            {                l = mid + eps;            }            else                r = mid - eps;        }        printf("%.3lf\n",l);    }    return 0;}


0 0
原创粉丝点击