2012-06-07 21:54:03Morris
[UVA][二分] 10566 - Crossed Ladders
Problem H
Crossed Ladders
Input: Standard Input
Output: Standard Output
Time Limit: 1 Second
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 的解
利用相似形的觀念可以得到當距離 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;
}
#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;
}