G - Light Bulb(幾何/三分)

G - 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, 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

題意:

https://vjudge.net/contest/277059#problem/G 對着題中的圖看,可以假設人距離燈x米,那麼每一個距離都可以利用幾何關係得到一個L‘(影子的長度)

x=D(HD)/Hx=D-(H*D)/H 的的時候剛好影子的頭部在牆角處,所以,根據幾何關係求出來長度f關於x的關係式

f(x)=xh/(Hh)...........x&lt;=DhD/Hf(x)=x*h/(H-h)...........當x&lt;=D-h*D/H

f(x)=D+HxD(Hh)/x............x&gt;=DhD/Hf(x)=D+H-x-D(H-h)/x............當x&gt;=D-h*D/H

對應第一種情況x的範圍是[0,DhD/H][0,D-h*D/H] 故最大值在端點處取

對應第二種情況x的取值範圍爲[DhD/H,D][D-h*D/H,D],求導以後最大值點對應的x=D(Hh)x=\sqrt{D*(H-h)}

如果極值點在該區間則最大值在該點,否則,最大值在兩端取。

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+10;
const int branch=26;
const int inf=0x7fffffff;
const double wc=1e-6;
const ll MOD=1e9+7;
int t;
double H,h,D;
double f(double x)
{
    return D+H-x-D*(H-h)/x;
}
int main()
{
    double ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf %lf %lf",&H,&h,&D);
        ans=D*h/H;
         double xx=D-(h*D/H);
        double jx=sqrt(D*(H-h));
        if(xx<=jx&&jx<=D)
        {
            ans=max(ans,f(jx));
        }
        else
            ans=max(ans,f(D));
        printf("%.3lf\n",ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章