您的位置:首页 > 其它

ZOJ_3203_LightBulb

2016-02-18 20:40 477 查看
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 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


Author: GUAN, Yao

Source: The 6th Zhejiang Provincial Collegiate Programming Contest

题目意思求如图所示的影子最大长度

显然当影子到达墙壁弯折之前的任意状态长度不如刚好到达墙壁时

影子到达墙壁后 墙壁上的影子变长

而地上的影子变短

因此影子长度应该为一个凸函数,三分求最大值就可以了

这里选取了人与灯的水平距离x为变量

这个题目精度要设置的高些 1e-8WA了一发……

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

const double AC=1e-9;
double H,h,D;

double f(double x)
{
double s1=H*x/(H-h);
double s2=(s1-D)*H/s1;
return D-x+s2;
}
double ts()
{
double lo=(H-h)*D/H,hi=D;
double mid,midmid;
while(hi-lo>AC)
{
mid=(lo+hi)/2;
midmid=(mid+hi)/2;
//cout<<"ts "<<mid<<" "<<midmid<<endl;
if(f(mid)>f(midmid))
hi=midmid;
else
lo=mid;
//cout<<endl;
}
return f(lo);
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&H,&h,&D);
printf("%.3lf\n",ts());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: