NPY and shot
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.99
Hint
If the height of NPY is 0,and he throws the shot at the 45° angle, he can throw farthest.
Source
题意:给你一个高度h,一个初始的速度v ;
你选择一个角度扔使其最远;
思路:三分抛出的角度;
显然函数成一段凸的抛物线形状;
代码有注释;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define mk make_pair #define eps 1e-7 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e6+10,M=4e6+10,inf=2147483647; const ll INF=1e18+10,mod=1e9+7; /// 数组大小 double h,v; double equ(double x) { double G=9.8,vx=v*cos(x/180*pi),vy=v*sin(x/180*pi); double t=vy/G;// 到达最高点的时间 t+=sqrt(2*G*h+vy*vy)/G; //到达顶端时间 // 2*G*h+vy*vy为到达最下端的速度的平方 return vx*t; } double ternarySearch(double l,double r) { while(r-l>eps) { double L=(2*l+r)/3; double R=(l+2*r)/3; double ans1=equ(L); double ans2=equ(R); if(ans1<ans2) l=L; else r=R; } return equ(l); } int main() { //cout<<sin(90.0/180*pi)<<endl; int T; scanf("%d",&T); while(T--) { scanf("%lf%lf",&h,&v); double l=0,r=90.0; printf("%.2f\n",ternarySearch(l,r)); } return 0; }