Problem 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 4Sample Output
1.000
0.750
4.000
题意:t 组数据,每组给出 H、h、D 三个数,H 代表灯泡高度,h 代表人高度,D 代表灯和墙的水平距离,求人从灯泡正下方走到墙壁的过程中其影子的长度 L 的最大值,其中影子的的长度 L 是地上的影子与墙上的影子的加和
思路:
过墙的右下角,画一条平衡与光线的辅助线,并设墙上的影子为 y,地上的影子为 x,这样一来,有:L=x+y
根据上图,可以利用相似三角形的性质得出:
那么,假设没有墙的情况下,有:
再根据 L=x+y,有:
可以看出,关于 L 的函数是一个在 [0,D] 上的凸函数,要求其最大值,利用三分法即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
double H,h,D;
double cal(double x){
double tanA=(H-h)/x;
double d=H/tanA;
if(d<=D)
return d-x;
d=d-D;
return D-x+d*tanA;
}
int main() {
int t;
scanf("%d",&t);
while(t--){
scanf("%lf%lf%lf",&H,&h,&D);
double left=0,right=D;
while(right-left>=EPS){
double lmid=left+(right-left)/3.0;
double rmid=right-(right-left)/3.0;
if(cal(lmid)<cal(rmid))
left=lmid;
else
right=rmid;
}
printf("%.3lf\n",cal(left));
}
return 0;
}