ZOJ-3203-Light Bulb【6th浙江省赛】【数学公式】【三分】

ZOJ-3203-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

题目链接:ZOJ-3203

题目大意:如图,求L的最大值

题目思路:详见代码。

以下是代码:

三分法:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define EPS 1e-9
double H,h,D,d;
double getL(double d)
{
    return D - d + H - (H - h) * D / d;
}
int main(){
    int t;
    cin >> t;
    while(t--)
    {   
        cin >> H >> h >> D;
        double l = (H - h) * D / H,r = D;
        while(r - l > EPS)
        {
            double mid = (l + r) / 2;
            double mid2 = (mid + r) / 2;
            if (getL(mid) < getL(mid2)) l = mid;
            else r = mid2;
        }
        printf("%.3f\n",getL(l));
    }
    return 0;
}

公式法:

待补充。

你可能感兴趣的:(数学,ZOJ,3203)