参考博客链接:「一本通 1.2 练习 3」灯泡(三分)
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
double h,d,h1;
double s(double x)
{
return (d-x)+h-(h-h1)*d/x;
}
int main()
{
int t,i,j;
double l,r;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&h,&h1,&d);
l=(h-h1)*d/h,r=d;
while(r-l>=1e-6)
{
double mid1=l+(r-l)/3.0;
double mid2=r-(r-l)/3.0;
if(s(mid1)>s(mid2))
r=mid2;
else
l=mid1;
}
printf("%.3lf\n",s(r));
}
}