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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const double EPS = 1e-6; double H, h, d; double Calc (double x) { return (h - x) * d / (H - x) + x; } int main() { // ifstream cin ("aaa.txt"); int t; cin >> t; while (t--) { cin >> H >> h >> d; double low = 0, high = h, mid, midmid; while (low + EPS < high) { mid = low + (high - low) / 2; midmid = mid + (high - mid) / 2; double midval = Calc (mid); double midmidval = Calc (midmid); if (midval < midmidval) low = mid; else high = midmid; } cout << fixed << setprecision (3) << Calc (low) << endl; } return 0; } |
- « 上一篇:zoj2913
- zoj3609:下一篇 »