Zoj3203 Light Bulb ----- three-point review

Source: Internet
Author: User

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 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 wide, 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
 
The equation can be listed as follows: the length of the Shadow located on the ground is l1, l1/D = (h-L)/(H-L), and l1 = (h-L) is obtained) * D/(H-L), so the total length of the shadow is l1 + L = (h-L) * D/(H-L) + L, only one unknown L, divide L into three points in the range of (0, h.
[Cpp]
# Include <iostream>
# Include <cstdlib>
# Include <stdio. h>
# Include <algorithm>
# Include <math. h>
# Define eps 1e-15
Using namespace std;
Double H, h, D;
Double cal (double xx)
{
Double ans = (h-xx) * D/(H-xx) + xx;
Return ans;
}
Double solve ()
{
Double left, right;
Double mid, midmid;
Double mid_area, midmid_area;
Left = 0; right = h;
While (left + eps <right)
{
Mid = (left + right)/2;
Midmid = (mid + right)/2;
Mid_area = cal (mid );
Midmid_area = cal (midmid );
If (mid_area <midmid_area) left = mid;
Else right = midmid;
}
Return cal (right );
}
Int main ()
{
Int t;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% lf", & H, & h, & D );
Double f = solve ();
// Double ss = cal (f );
Printf ("%. 3lf \ n", f );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.