HDU 5144 NPY and shot(物理+二分)

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

#include <iostream>
#include <cstdio>
#include <cmath>
#define PI acos(-1)
#define EPS 1e-7
#define g 9.8
using namespace std;
int h, v;
double x(double t) {//将整个运动分成两部分,斜抛+平抛
    double t1 = sin(t)*v/g;//斜抛的时间
    double t2 = sqrt((h+v*sin(t)*t1-0.5*g*t1*t1)*2/g);//平抛的时间
    return (t1+t2)*v*cos(t); //水平位移
}
int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &h, &v);
        double l = 0, r = PI/2, mid, mmid;
        while(r-l > EPS) {
            mid = (l + r) / 2;
            mmid = (mid + r) / 2;
            if(x(mid) >= x(mmid)) {
                r = mmid;
            } else {
                l = mid;
            }
        }
        printf("%.2lf\n", x(l));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Adusts/article/details/81565807