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
这题的关键是写出一个等式,这里用三角形的相似来得出一个等式 我们假设地下的影子的长度为 X ,这里有两个相似的直角三角形,他们的直角边的比值是 (H-L) / D = (h-L)/ x 等式化简有 X = D*(h-L) / (H-L) , 影子的全部长度为地上的长度加上墙上的长度L ,在上式两边同时加上 L 有 Y = X + L = D*(h-L) / (H-L) + L 这里的等式的未知数就是L ,L (墙上的长度)的范围肯定是 0 到 h ,那么,通过三分法求出L的值,即可得出答案;代码如下:
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<time.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<list>
#include<queue>
#include<map>
#define E exp(1)
#define PI acos(-1)
#define mod (ll)(1e9+9)
#define INF 0x3f3f3f3f;
#define MAX 40000
//#define _CRT_SECURE_NO_WARNINGS
//#define LOCAL
using namespace std;
typedef long long ll;
typedef long double lb;
int main(void)
{
#ifdef LOCAL
freopen("data.in.txt", "r", stdin);
freopen("data.out.txt", "w", stdout);
#endif
int t;
double H, h, D;
while (scanf("%d", &t) != EOF) {
for (int i = 1; i <= t; ++i) {
scanf("%lf%lf%lf", &H, &h, &D);
double le, ri, lmid, rmid, tmp_1, tmp_2;
le = 0, ri = h;
while (fabs(ri - le) > 1e-8) {
lmid = (ri + le) / 2;
rmid = (lmid + ri) / 2;
tmp_1 = D*(h - lmid) / (H - lmid) + lmid;
tmp_2 = D * (h - rmid) / (H - rmid) + rmid;
if (tmp_1 > tmp_2) ri = rmid;
else le = lmid;
}
double x = (ri + le) / 2;
double res = D * (h - x) / (H - x) + x;
double tmp = max(D * h / H, h);
res = max(tmp, res);
printf("%.3f\n", res);
}
}
// end = clock();
// cout << "using tmie:" << (double)(end - start) / CLOCKS_PER_SEC * (1000) << "ms" << endl;
//system("pause");
return 0;
}