您的位置:首页 > 其它

ZOJ - 3203 三分 Light Bulb 详细题解

2017-08-05 15:34 246 查看

三分 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, andH -
h >= 10-2.

<b< dd="">

Output

For each test case, output the maximum length of mildleopard's shadow in one line, accurate up to three decimal places..

<b< dd="">

Sample Input

3
2 1 0.5
2 0.5 3
4 3 4


<b< dd="">

Sample Output

1.000
0.750
4.000


详细题解:
该题是一道利用三分解决的问题,在这之前我们需要通过数学的相似三角形的知识来找到图示中的关系进而进行确认求解。假设我们利用人到灯的距离x当作自变量进
行分析。
人从灯下走到墙边的过程中,人的影子由全部在地上到一部分在地上一部分在墙上最后到全部在墙上,所以人影子的长度由逐渐变长到逐渐变短中间会出现一个最大
值,而这个最大值就是我们要求解的长度。这个过程放在坐标系下是一个先递增后递减的抛物线,故而采用三分进行求解。
当x是最小值(左边界)时,人的影子全部在地上:x=D-L=D-hD/H=D(H-h)/H;
当x是最大值(右边界)时,人的影子全部在墙上:x=D;即x的范围为(D(H-h)/H,D),左边界为left;右边界为right。精确度为1e-9。
利用数学中相似三角形的知识可以求出L关于x的方程:
  
z/h=k/(y+k)~~~~~~~~~~k=zy/(h-z);
z/H=k/(D+k)~~~~~~~~~k=zD/(H-z);
联立上述两个方程:
z=(yH-Dh)/(y-D)=H-D(H-h)/x;
由L=y+z可知:
L=y+z=D-x+H-D(H-h)/x;
利用 left 和 right 求出它们的中点 mid ;利用 right 与 mid 求出它们的中点 midmid 。通过不断缩小x 的范围求出当 x=mid 时,L的最大值。
注意结果要保留三位小数。

下面附上AC代码:

#include<stdio.h>
double D,H,h;
double l(double x){
return D-x+H-(H-h)*D/x;
}
int main(){
int t;
while(~scanf("%d",&t)){
while(t--){
scanf("%lf%lf%lf",&H,&h,&D);
double left=(H-h)*D/H,right=D,mid,midmid;
while(left+1e-9<=right){
mid=(left+right)/2;
midmid=(mid+right)/2;
if(l(mid)>=l(midmid))
right=midmid;
else
left=mid;
}
printf("%.3lf\n",l(mid));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ZOJ - 3203 Light Bulb