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 while
HIs 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.0000.7504.000
PS: Thanks to Momo's solution report
This topic is also impressive because it has been done before. It mainly introduces the formula and finds the longest shadow by three points.
As shown in the figure, the longest length of the shadow l is obtained when people move around.
Solution:
According to the figure, it is easy to find that when the light, the head and the corner of a person form a straight line, the length of this time is the maximum length of the shadow on the ground. When a person goes to the right, the shadow begins to be projected onto the wall. When a person sticks to the wall, the length of the shadow is the height of the person. So when the head, corner, and lamp are placed in a line to the wall, the function is to increase and decrease first, which is a convex function, so we can use three points to solve the problem.
The shadow length is BC + Ce, that is, L + D * (H-l)/(h-l)
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> # include <string> # include <algorithm> # include <map> using namespace STD; double H, H, D, sum; double calc (double L) {return L + D * (H-l)/(h-l);} int main () {int num; scanf ("% d", & num); While (Num --) {scanf ("% lf", & H, & H, & D); double left = 0, Right = H; while (left + 0.00001 <right) // triplicate solution {double mid = (left + right)/2; double mimid = (Mid + right)/2; If (calc (MID)> calc (mimid) Right = mimid; else left = mid;} printf ("%. 3lf \ n ", Calc (left);} return 0 ;}