http://acm.hdu.edu.cn/showproblem.php?pid=5144
Problem Description
NPY is going to have a PE test.One of the test subjects is throwing the shot.The height of NPY is H meters.He can throw the shot at the speed of v0 m/s and at the height of exactly H meters.He wonders if he throws the shot at the best angle,how far can he throw ?(The acceleration of gravity, g, is
9.8m/s2
)
Input
The first line contains a integer
T(T≤10000)
,which indicates the number of test cases.
The next T lines,each contains 2 integers H(0≤h≤10000m) ,which means the height of NPY,and v0(0≤v0≤10000m/s) , which means the initial velocity.
The next T lines,each contains 2 integers H(0≤h≤10000m) ,which means the height of NPY,and v0(0≤v0≤10000m/s) , which means the initial velocity.
Output
For each query,print a real number X that was rounded to 2 digits after decimal point in a separate line.X indicates the farthest distance he can throw.
Sample Input
2 0 1 1 2
Sample Output
0.10 0.99HintIf the height of NPY is 0,and he throws the shot at the 45° angle, he can throw farthest.
/**
hdu5144物理题+三分
题目大意:每次从一定高度以速度v扔一个小球,求在以最优的角度前提下小球水平可以飞出的距离。
解题思路:设抛出角度为a,将速度沿着水平和竖直方向分解,求出小球的滞空时间表达式,然后三分求解即可
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
double h,v;
double f(double a)
{
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.0;
while((r-l)>=1e-10)
{
double ll,rr;
ll=(l*2+r)/3;
rr=(l+r*2)/3;
if(f(ll)<f(rr))
l=ll;
else
r=rr;
}
printf("%.2lf\n",f(l));
}
return 0;
}