ZOJ 三分搜索 3203 Light Bulb
Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodious house, thinking of how to earn more money. One day, he found that the length of his shadow was changing from time to time while walking between the light bulb and the wall of his house. A sudden thought ran through his mind and he wanted to know the maximum length of his shadow.
Input
The first line of the input contains an integer T (T <= 100), indicating the number of cases.
Each test case contains three real numbers H, h and D in one line. H is the height of the light bulb while h is the height of mildleopard. D is distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both inclusive, and H - h >= 10-2.
Output
For each test case, output the maximum length of mildleopard's shadow in one line, accurate up to three decimal places..
Sample Input
3 2 1 0.5 2 0.5 3 4 3 4
Sample Output
1.000 0.750 4.000
解题代码:
1 // File Name: Light Bulb 3203.cpp 2 // Author: sheng 3 // Created Time: 2013年05月12日 星期日 19时14分03秒 4 5 #include <iostream> 6 #include <stdio.h> 7 #include <math.h> 8 using namespace std; 9 10 double H, h, D; 11 const double EPS = 1e-10; 12 double temp_ans (double x) 13 { 14 return D*(h-x)/(H-x) + x; 15 } 16 17 int main () 18 { 19 int cas; 20 double left, right; 21 double mid, midmid; 22 double mid_value, midmid_value; 23 scanf ("%d", &cas); 24 while (cas--) 25 { 26 cin >> H >> h >>D; 27 left = 0; 28 right = h; 29 while (left + EPS < right) 30 { 31 mid = (left + right)/2; 32 midmid = (mid + right)/2; 33 mid_value = temp_ans(mid); 34 midmid_value = temp_ans(midmid); 35 if (mid_value > midmid_value) 36 right = midmid; 37 else left = mid; 38 } 39 printf ("%.3lf\n", temp_ans(left)); 40 } 41 return 0; 42 }