ZOJ - 3203 Light Bulb (数学公式,或者,三分)

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 Hh 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

比赛时  推得公式,  有用三分过的.,一块把 三分给补了;  三分666  之前 写过一次,  就再也没碰到了

做个图把


一共三种情况,  :  第一种  人在墙边上,  影子就是 就是h,  第二种  影子全在地上 就是  (h/H )*D   下面是个三角形,相似, 

 

   第三种 就是  墙上和地上 都有,    把矩形补齐, 上面是个三角形, 设h-H之间距离为y   墙上为x

则 影子长度为  D-y+x    根据三角形相似: 有


三种情况取最大值就可以了, 注意第三种 要满足情况

[代码实现]

#include <iostream>
#include <string.h>
#include <cmath>
#include <stdio.h>
using namespace std;
const int inf=0x3f3f3f3f;
int dir[4][2]={1,0,0,1,0,-1,-1,0};

int main()
{
    int t;
    cout.precision(3);
    cin>>t;
    while (t--)
    {
     double H,h,D;
     cin>>H>>h>>D;
     double ans=max(h,h/H*D);
     if(H-sqrt(D*(H-h))<h&&H-sqrt(D*(H-h))>0)
        ans=max(ans,D+H-2*sqrt(D*(H-h)));
    printf("%.3lf\n",ans);
    }
    return 0;
}

二分应用于 单调函数,  三分应用于 极值问题,   

三分 左端点就是D-(H-h)*D  右端点 D;  注意 三分返回 右边,   左边 神奇的WA了

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>

using namespace std;
const int inf=0x3f3f3f3f;
const double esp=1e-8;
int dir[4][2]={1,0,0,1,0,-1,-1,0};
double H,h,D;
double Cal(double y)
{
	return D+H-y-(H-h)*D/y;
}
double three_search(double l,double r)
{
	while(l+esp<=r)
	{
		double mid1=(l+r)/2.0;
		double mid2=(mid1+r)/2.0;
		if(Cal(mid1)>=Cal(mid2))
			r=mid2;
		else
			l=mid1;
	}
	return Cal(r);
}
int main()
{
	int t;
    cin>>t;
    while (t--)
    {
		cin>>H>>h>>D;
		printf("%.3lf\n",three_search(D-h/H*D,D));
    }
    return 0;
}


12

posted @ 2018-02-02 10:48  Sizaif  阅读(177)  评论(0编辑  收藏  举报