Toxophily HDU - 2298 三分+二分

题目:https://vjudge.net/contest/364745#problem/B

先用三分求出最高点Y,然后在进行二分,求出角度

注意写法   PI的弧度是  

acos(-1)/2-EPS接近90度的时候相当于除以0
二分的精度可以用迭代次数来保证,比如100次
#include <iostream>
#include <cmath>
using namespace std;
const double EPS = 1e-8;
int T; double x, y, v;
double f(double a)//角度为a时对应的高度
{
    double t = x/(v*cos(a));
    return v*sin(a)*t - 9.8/2*t*t;
}
int main()
{
    cin >> T;
    while(T--)
    {
        cin >> x >> y >> v;
        double L = 0, R = acos(-1)/2-EPS;
        if(x==0) //特判,否则三角函数会智障掉
        {
            if(v*v/2/9.8 > y) printf("%.6lf\n", R);
            else printf("-1\n");
            continue;
        }
        for(int i=1;i<=100;i++)
        {
            double mid_L = (L+R) / 2;
            double mid_R = (mid_L+R) / 2;
            if(f(mid_L) > f(mid_R))
            {
                R = mid_R;
            } else {
                L = mid_L;
            }
        }
        if(f(L) < y) {printf("-1\n"); continue;}
        R = L, L = 0;
        for(int i=1;i<=100;i++)
        {
            double mid = (L+R)/2;
            if(f(mid) < y)
            {
                L = mid;
            } else {
                R = mid;
            }  
        }
        printf("%.6lf\n", L);
    }
}

 

posted @ 2020-04-09 23:55  SunCY  阅读(138)  评论(0编辑  收藏  举报