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 have only one light bulb in the He house. Every night, he is wandering in his incommodious house, and thinking of how to earn. One day, he found that's the length of his shadow is changing from time to time while walking between the light bulb and th E 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 <=), indicating the number of cases.
Each test case contains three real numbers H, H and D on one line. h is the height of the bulb while H is the height of mildleopard. D is distance between, the light bulb and the wall. All numbers is in range from 10-2 to 103, both inclusive, and H - H >= 10-2.
Output
For each test case, the output of the maximum length of Mildleopard ' s shadow on one line, accurate up to three decimal places.
Sample Input
32 1 0.52 0.5 34 3 4
Sample Output
1.0000.7504.000
Solution: Find the function, two methods, one derivation directly, the other is three points; Law one:
1#include <stdio.h>2#include <math.h>3 intMain () {4 intT;5 Doubleh,h,d;6 //formula: d-x+h-(h-h) *d/x;7 //derivation: -1+ (h-h) *d/(x*x)8 //the derivative equals 0, and the Extremum x=sqrt ((h-h) *d) is obtained.9 //x= (h-h) *d/h when the shadow is longest on the ground;Tenscanf"%d",&T); One while(t--){ Ascanf"%LF%LF%LF",&h,&h,&D); - DoubleJz=sqrt ((h-h) *D); - Doubleans; the if(jz<= (h-h) *d/h) ans=d-(h-h) *d/H; - Else if(Jz>=d) ans=h; - Elseans=d-jz+h-(h-h) *d/JZ; -printf"%.3lf\n", ans); + } - return 0; +}
Three points:
1#include <stdio.h>2#include <math.h>3 Doubleh,h,d;4 DoubleGetl (Doublex) {5 returnd-x+h-(h-h) *d/x;6 }7 voidSanfen () {8 DoubleL= (h-h) *d/h,m,mm,r=d;//L start from the longest ground9 while(r-l>1e-Ten){TenM= (L+R)/2; OneMm= (M+R)/2; A if(Getl (m) >=getl (mm)) r=mm; - ElseL=m; - } theprintf"%.3lf\n", Getl (L)); - } - intMain () { - intT; +scanf"%d",&T); - while(t--){ +scanf"%LF%LF%LF",&h,&h,&D); A Sanfen (); at } - return 0; -}
Light Bulb (three points)