1004

原题:
Problem Description
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

题目大意:
给出要射中的坐标和初速度,判断能射中的角度,不能射中返回-1

形成过程:

这题估计要用二分
角度从0和pi/2之间
判断当横坐标与目标点相同时纵坐标与目标点的上下关系

感想:

精度很重要!精度很重要!精度很重要!重要的话说三遍
在精度为1e-6的时候WA,改成1e-8 AC;

代码(注释是做题的时候写的):

/* 这题估计要用二分 角度从0和pi/2之间 判断当横坐标与目标点相同时纵坐标与目标点的上下关系 注意:精度很重要,在精度为1e-6的时候WA,改成1e-8AC; */

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
    freopen("nrj.txt","r",stdin);
    const double pi=3.14159265358979; 
    int times;
    double x,y,v;
    double max,min,mid;
    double vx,vy;
    double t,h;
    cin>>times;
    while(times--)
    {
        max=pi/2;
        min=0;
        mid=(max+min)/2;
        cin>>x>>y>>v;
        while(max-min>1e-8)
        {
            vx=v*cos(mid);
            vy=v*sin(mid);
            t=x/vx;
            h=vy*t-9.8/2*t*t;
            if(h>=y)
                max=mid;
            else
                min=mid;

            mid=(max+min)/2;
        }
        if(mid<1e-8||pi/2-mid<1e-8)
            cout<<"-1"<<endl;
        else
            printf("%.6f\n",mid);
  }

}

你可能感兴趣的:(1004)