Crossed ladders---poj2507(二分+简单几何)

题目链接:http://poj.org/problem?id=2507

 

题意就是给你x y c求出?的距离;

h1 = sqrt(x*x-d*d);

h2 = sqrt(y*y-d*d);

(h1-c)/h1 = d1/d = c/h2

c = (h1*h2)/(h1+h2);

二分找到d即可;

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define N 2510
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL;

int main()
{
    double x, y, c;

    while(scanf("%lf %lf %lf", &x, &y, &c)!=EOF)
    {
        double L = 0, R = min(x, y);
        while(L <= R)
        {
            double Mid = (L+R)/2;
            double h1 = sqrt(x*x-Mid*Mid);
            double h2 = sqrt(y*y-Mid*Mid);
            double temp = h1*h2/(h1+h2);
            if(fabs(temp - c)<1e-5)
            {
                printf("%.3f
", Mid);
                break;
            }
            else if(temp > c)
                L = Mid;
            else
                R = Mid;
        }
    }
    return 0;
}

 

原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5545742.html