NPY and shot
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 761 Accepted Submission(s): 309
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.
Source
给出初速度V和高度h,问球运动的最大距离s。
解题思路:如图,球扔的距离s与投掷角度a有关系,存在确定的函数关系式,并且函数符合单峰性质,因此三分角度就可以了。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#include <ctime>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const double g=9.8;
double v,h;
double dis(double a)
{
double vx=v*cos(a);
double vy=v*sin(a);
return (vy/g+sqrt((vy*vy/(2*g)+h)*2/g))*vx;
}
int main()
{
int t;
double l,r,mid1,mid2;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf",&h,&v);
l=0;
r=pi/2;
while(r-l>=eps)
{
mid1=(l+r)/2;
mid2=(mid1+r)/2;
if (dis(mid1)<dis(mid2))
l=mid1;
else
r=mid2;
}
printf("%.2f\n",dis(l));
}
return 0;
}