hdu 5144 NPY and shot(三分)

http://acm.hdu.edu.cn/showproblem.php?pid=5144

 

题意 :给出初速度 v 和高度 h

         求最远斜抛距离

 

思路:根据物理公式推出 距离  s=v*cos(x)*(t1+t2);

        再对x从0 到π/2 进行三分

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
double g,h,v;
double fun(double x)
{
    double t1=v*sin(x)/g;
    double y=v*sin(x)*t1-g*t1*t1/2;
    double t2=sqrt(2*(h+y)/g);
    return v*cos(x)*(t1+t2);
}
int main()
{
    g=9.8;
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%lf%lf",&h,&v);
        double midl,midr;
        double l=0,r=acos(-1.0);
        while(r-l>1e-7)
        {
            midl=l+(r-l)*1/3;
            midr=l+(r-l)*2/3;
            if(fun(midl)>fun(midr)) r=midr;
            else l=midl;
        }
        printf("%.2f\n",max(fun(l),fun(r)));
    }
    return 0;
}
View Code

 

posted @ 2015-01-26 13:16  sola94  阅读(100)  评论(0编辑  收藏  举报