Zoj problem set-3203 Light bulb
Time limit:1 second Memory limit:32768 KB
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 integerT(T<= 100), indicating the number of instances.
Each test case contains three real numbersH,HAndDIn one line.HIs the height of the light bulb whileHIs the height of mildleopard.DIs distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both wide, andH-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
32 1 0.52 0.5 34 3 4
Sample output
1.2.16.7504.000
Originally, this question directly introduced the formula and directly found the most value point.
But when I saw that I had to use the three-way method on the Internet, I reviewed the three-way method. Because it was convex, I used the three-way method.
Formula:
# Include <stdio. h> # Include < String . H> # Include <Math. h> # Include <Iostream> # Include <Algorithm> Using Namespace STD; Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int T; Double H, H, D; scanf ( " % D " ,& T ); While (T -- ) {Scanf ( " % Lf " , & H, & H ,& D ); Double Temp = SQRT (h-h )* D ); Double Temp2 = (h-h) * D/ H; If (Temp> = d) printf ( " %. 3lf \ n " , H ); Else If (Temp <temp2) printf (" %. 3lf \ n " , H * D/ H ); Else { Double Ans = d + H-temp-(h-h) * D/ Temp; printf ( " %. 3lf \ n " , ANS );}} Return 0 ;}
Method 3:
# Include <stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> # Include <Math. h> Using Namespace STD; Const Double EPS = 1E- 9 ; Double D, H, H; Double Calc ( Double X ){ Return D-x + H-(h-h) * D/ X ;} Double Solve ( Double L, Double R ){ Double Mid, midmid; Double D1, D2; Do {Mid = (L + r )/ 2 ; Midmid = (Mid + r )/ 2 ; D1 = Calc (MID); d2 = Calc (midmid ); If (D1> = d2) r = midmid; // Note that Else L = Mid ;} While (R-l> = EPS ); Return D1 ;} Int Main (){ Int T; scanf ( " % D " ,& T ); While (T -- ) {Scanf ( " % Lf " , & H, & H ,& D); printf ( " %. 3lf \ n " , Solve (h-h) * D/ H, D ));} Return 0 ;}