0
点赞
收藏
分享

微信扫一扫

ZOJ 3203 Light Bulb(三分)

yeamy 2022-06-17 阅读 23

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

因为人从灯下一直走到影子到墙角的时候,影子长度都是递增的,所以让区间在影子刚好到墙角时的人的位置到人距离灯D距离。
并且因为calc函数里面是假定影子在墙上一定有一段距离的时候,那么定义域的l端点必须是影子刚好到达墙上的临界点了。

#include <iostream>
#include <cstdio>
#include <stack>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-10;

double H, h, D;
double calc(double x)
{
return D-x+H-(H-h)*D/x;
}
int main()
{
int t;
scanf("%d", &t);
while (t--){
scanf("%lf%lf%lf", &H, &h, &D);
double l=D-h*D/H, r=D, ans;
while (r-l>eps){
double mid1=l+(r-l)/3;
double mid2=l+(r-l)/3*2;
if (calc(mid1)>=calc(mid2))
r=mid2;
else
l=mid1;
}
printf("%.3f\n", calc(l));
}
return 0;
}


举报

相关推荐

0 条评论