NPY and shot
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 541 Accepted Submission(s): 211
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/s
2![]()
)
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
v
0
(0≤v
0
≤10000m/s)
, which means the initial velocity.
The next T lines,each contains 2 integers H(0≤h≤10000m)
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.
题意:
扔铅球
题目描述
一个人身高为h m,他能使铅球以v0 m/s的固定初速度(在高h处)飞出去,问他应以怎样的最佳角度扔,让球飞最远。重力加速度g取9.8m/s^2。
输入格式
第一行一个数T(T<=10000),表示数据组数,接下来T行,每行两个整数h(0<=h<10000),v0(0<=v0<10000),表示每一组询问。
输出格式
每行一个实数,表示该组询问对应的能扔的最远距离x m,保留2位小数,四舍五入。
思路:首先用h,v,p(抛出角度),g来表示抛出距离x。x=v*v*sin(p)*cos(p)/g+v*cos(p)*sqrt((2*h/g)+v*v*sin(p)*sin(p)/g/g);
明显是单峰三分角度求出答案,角度范围为0到π/2,精度为1e-7。
拓展:精度s的确定方法,vmax=1e+4,hmax=1e+4,sinx斜率最大为1,则认为sin(x2)-sin(x1)=x2-x1=s。因为f(x2)的误差不能超过0.001,所以s*1e+4<=1e-3,所以s<=1e-7.
AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<math.h>
using namespace std;
#define PI9 asin(1.0)
double h,v,g=9.8;
double f(double p){
return v*v*sin(p)*cos(p)/g+v*cos(p)*sqrt((2*h/g)+v*v*sin(p)*sin(p)/g/g);
}
int main(){
double l,r;
int t;
cin>>t;
while(t--){
cin>>h>>v;
l=0,r=PI9;
while(r-l>1e-7){
double mid1=(r+l)/2;
double mid2=(mid1+r)/2;
if(f(mid1)<f(mid2)) l=mid1;
else r=mid2;
}
printf("%.2lf\n",f(r));
}
return 0;
}