In monotone functions, we often use dichotomy to quickly search for feasible solutions. In the single-peak function, we use the three-way method to search for the extremum of the function. In general, the three-way method can be divided evenly, or the first point points at the midpoint of the left and right end points, the second point at the midpoint of the midpoint and the end.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespaceStd;
Double H,V;
Double F(DoubleA)
{
Double G=9.8,Vx=V*Cos(A),Vy=V*Sin(A);
return Vx*(Vy/G+sqrt((2*H*G+Vy*Vy)/(G*G)));
}
int Main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%LF%LF",&H,&V);
Double L=0,R=90.00;
while((R-L) >=1e-10)
{
Double ll,Rr;
ll=(L+R)/2;
Rr=(ll+R)/2;
If (F(ll) <F(Rr))
L=ll;
else
R=Rr;
}
printf("%.2lf\n",F(L));
}
return 0;
}
Three-way, a school sister asked me what is the three-point method, why should have a three-way