24h購物| | PChome| 登入
2012-06-07 21:54:03| 人氣898| 回應0 | 上一篇 | 下一篇

[UVA][二分] 10566 - Crossed Ladders

推薦 0 收藏 0 轉貼0 訂閱站台

Problem H
Crossed Ladders
Input: Standard Input

Output: Standard Output

Time Limit: 1 Second

 

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?

Each line of input contains three positive floating point numbers giving the values of x, y, and c.

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                             Output for Sample Input

30 40 10
12.619429 8.163332 3
10 10 3
10 10 1
 
26.033
7.000
8.000
9.798


做法 : 二分答案
利用相似形的觀念可以得到當距離 d 被決定的時候, c 的解

#include <stdio.h>
#include <math.h>
#define eps 1e-5

int main() {
    double c, x, y;
    while(scanf("%lf %lf %lf", &x, &y, &c) == 3) {
        double l = 0, r = x < y ? x : y, d, tc;
        while(1) {
            d = (l+r)/2;
            tc = sqrt(y*y-d*d)*sqrt(x*x-d*d)/(sqrt(y*y-d*d)+sqrt(x*x-d*d));
            if(fabs(tc-c) <= eps)
                break;
            if(tc > c)
                l = d;
            else
                r = d;
        }
        printf("%.3lf\n", d);
    }
    return 0;
}

台長: Morris
人氣(898) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11094 - Continents
此分類上一篇:[UVA][幾何] 833 - Water Falls

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文