HDU 5144 NPY and shot【物理+三分查找】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37867156/article/details/81562386

题目链接: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.

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.

三分查找:https://www.cnblogs.com/ECJTUACM-873284962/p/6536414.html

题解:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define LL long long
#define PI acos(-1)
#define g 9.8
using namespace std;
const int maxn=1005;
double cal(int h0,int v,double angle){
	double vx=v*cos(angle);
	double vy=v*sin(angle);
	double t=vy/g;
	double s=vx*t;
	double h=h0*1.0+0.5*g*t*t;
	t=sqrt(2.0*h/g) ;
	s+=t*vx;
	return s;
}
double search(int h0,int v){
	double l=0.0,r=(PI*1.0)/2;
	double mid,midmid;
	while(r-l>1e-8){
		mid=(l+r)/2.0;
		midmid=(mid+r)/2.0;
		if(cal(h0,v,mid)>=cal(h0,v,midmid))
			r=midmid;
		else l=mid;
	}
	return cal(h0,v,l);
}
int main(){
	int T,H,v0;
	scanf("%d",&T) ;
	while(T--){
		scanf("%d %d",&H,&v0);
		printf("%.2lf\n",search(H,v0));
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37867156/article/details/81562386