原函数为单峰函数,所以不能用二分。
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
const double pi = acos(-1.0);
const double g = 9.8;
double h, v, l, r, ml, mr, k;
double f(double ang)
{
double a = -g / 2, b = v * sin(ang), c = h;
double d = sqrt(b * b - 4 * a * c);
double t = -(b + d) / (2 * a);
return cos(ang) * v * t;
}
int main()
{
// freopen("C.in", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lf%lf", &h, &v);
l = 0, r = pi/2;
for(int i = 0; i < 60; i++)
{
k = (r - l) / 3;
ml = l + k, mr = r - k;
if (f(ml) < f(mr)) l = ml;
else r = mr;
}
printf("%.2lf\n", f(l));
}
return 0;
}