Description
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
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
二分法适合解决单调性一致的问题,而如果单调性不一致,就要用三分法。
当需要求某凸性或凹形函数的极值,通过函数本身表达式并不容易求解时,就可以用三分法不断逼近求解。
基本模型
题目大意:求人从左向右走动时,影子的长度L的最大值;
很容易发现当人在灯正下方时影子最短,从灯正下方往右走,影子长度逐渐增大,当人再向右走时,影子开始投影到墙上,当人贴着墙,影子长度即为人的高度,所以影子的长度又逐渐减小。所以当人从灯正下方点走到墙,函数是先递增再递减,为凸性函数,所以我们可以用三分法来求解。
我们假设:人距离灯的水平距离为x,则不难推出:随着x的变化,L = D - x + H - D * (H - h) / x; 是先增后减函数。
我们立足于求其最大值。
x的初始值我们可以假设为:D - D*h/H,因为从0开始到此,L肯定是递增的,所以不必考虑先前的,x最大值为D。
#include<iostream>
#include<cstdio>
using namespace std;
double D,H,h;
double cal(double x)
{
return D-x+H-D*(H-h)/x;
}
double sove(double l,double r)
{
double k1,k2,mid,midd;
do
{
mid=(l+r)/2;
midd=(mid+r)/2;
k1=cal(mid);
k2=cal(midd);
if(k1>=k2)
r=midd;
else
l=mid;
}
while(r-l>=1e-10);
return k1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>H>>h>>D;
printf("%.3lf\n",sove((H-h)*D/H,D));
}
}