Toxophily(二分+三分)

Toxophily

The recreation center of WHU ACM Team has indoor billiards, Ping Pang, chess and bridge, toxophily, deluxe ballrooms KTV rooms, fishing, climbing, and so on.
We all like toxophily.

Bob is hooked on toxophily recently. Assume that Bob is at point (0,0) and he wants to shoot the fruits on a nearby tree. He can adjust the angle to fix the trajectory. Unfortunately, he always fails at that. Can you help him?

Now given the object’s coordinates, please calculate the angle between the arrow and x-axis at Bob’s point. Assume that g=9.8N/m.
Input
The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case is on a separated line, and it consists three floating point numbers: x, y, v. x and y indicate the coordinate of the fruit. v is the arrow’s exit speed.
Technical Specification

  1. T ≤ 100.
  2. 0 ≤ x, y, v ≤ 10000.
    Output
    For each test case, output the smallest answer rounded to six fractional digits on a separated line.
    Output “-1”, if there’s no possible answer.

Please use radian as unit.
Sample Input

3
0.222018 23.901887 121.909183
39.096669 110.210922 20.270030
138.355025 2028.716904 25.079551

Sample Output

1.561582
-1
-1

思路:
题目是说,从点(0,0)射击到(x,y),射中的最小角度。可以二分逼近出一个结果,判断他能否射中,找到能射中的最大高度,然后和y比较,小于y就是射不中,否则就再二分逼近结果。
设 x 表示水平位移,y 表示竖直位移,t 表示运动时间。可以得式子:
x = v * cos(a) * t;
y = v * sin(a) * t;
两个式子联立,消去 t 得:y = x*tan(a)- x * g/(2 * v^2 * cos(a)*cos(a));一个 x 关于 y 的函数,然后用三分法。
完整代码:

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double g=9.8;
const double eps=1e-9;
const double pi=acos(-1.0);
double x,y,v;

double f(double n)
{
    double a = x*tan(n)-x*x*g/(2*v*v*cos(n)*cos(n));
    return a;
}

double thrp(double n)       //三分
{
    double l=0.0,r=n;
    double m1,m2;
    while(r-l>=eps)
    {
        m1=(l+r)/2;
        m2=(m1+r)/2;
        if(f(m2)-f(m1)>=eps)
        {

            l=m1;
        }
        else
        {
            r=m2;
        }
    }
    return (m1+m2)/2;
}

double binary(double n)         //二分
{
    double l=0.0;
    double r=n;
    double bet;
    while(r-l>eps)
    {
        bet = (l+r)/2.0;
        if(f(bet)<y)
        {
            l=bet;
        }
        else
        {
            r=bet;
        }
    }
    return bet;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>v;
        double n=thrp(0.5*pi);
        if(f(n)<y)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            printf("%.6lf\n",binary(n));
        }
    }
    return 0;
}
发布了64 篇原创文章 · 获赞 66 · 访问量 2590

猜你喜欢

转载自blog.csdn.net/qq_45856289/article/details/104598801
今日推荐