【三分】light bulb(zoj3203)

题目描述:

如图,你可以在房间里移动,灯泡的高度为H,你的身高为h,灯泡与墙的水平距离为D,求你影子的最长长度(影子长度=地上影子长度+墙上影子长度)

样例输入:

3
2 1 0.5
2 0.5 3
4 3 4

样例输出:

1.000

0.750

4.000

此题是一道三分模板题

用x表示墙上影子的长度,根据相似三角形可以得到地上的影子长度为(h-x)*d/(H-x)

如图:

三分答案求x即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

inline int read()
{
   int f=1,x=0;
   char ch=getchar();
   while(ch<'0' || ch>'9') {if(ch=='-') f=-1; ch=getchar();}
   while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
   return x*f;
}

int T;
double H,h,d;

double f(double x)
{
    return (h-x)*d/(H-x)+x;
}

int main()
{
    T=read();
    while(T--)
    {
        scanf("%lf%lf%lf",&H,&h,&d);
        double l=0,r=h;
        while(r-l>1e-6)
        {
            double lm=l+(r-l)/3,rm=r-(r-l)/3;
            if(f(lm)>=f(rm)) r=rm;
            else l=lm;
        }
        printf("%.3lf\n",f(r));
    }
    return 0;
}
这是代码

猜你喜欢

转载自www.cnblogs.com/llllllpppppp/p/9334762.html