Toxophily

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
题意:鲍勃射箭(注意实在平面射箭,刚开始我也理解错了,但是后来看到没有高度所以,不可能在空间),鲍勃在(0,0),给出苹果的坐标,和箭飞的速度,求最小角度;
解题思路:现在0-pi之间用三分求出最大角度,要是最大角度都飞不到苹果的地方就不可能射到就输出-1,否则,再在0-在大角度之间求最小角度;
感悟:二分这里没什么解题思路可以写,无非就是控制精度的,懂了题意就用二分或三分解;
代码(g++)
#include
#include
#include
#define g 9.8
#define pi 3.1415926535897932384626433832795028841971693993751058209
//这次就不听pi精度还不够
double x,y,v;
double  distance_y(double s)
{
    return v*sin(s)*x/(v*cos(s))-4.9*x*x/(v*cos(s)*v*cos(s));
}
int main()
{
    //freopen("in.txt", "r", stdin);
    double mid1,mid2,first,endn,sum1,sum2;
    int n;
    scanf("%d",&n);
    for(int i=0;i
    {
        scanf("%lf%lf%lf",&x,&y,&v);
        first=0;
        endn=pi;//再大就反向Q了;
        while(fabs(endn-first)>1e-8)//三分来判断最大的那个角度
  {
   mid1=first+(endn-first)/3;
   mid2=endn-(endn-first)/3;
   sum1=distance_y(mid1);
   sum2=distance_y(mid2);
   if(sum1>sum2)
    endn=mid2;
   else
    first=mid1;
  }
  if(distance_y(first)
  {
   printf("-1\n");
   continue;
  }
  first=0;
  while(fabs(endn-first)>1e-8)//二分找最小值
  {
   mid1=(endn+first)/2;
   sum1=distance_y(mid1);
   if(sum1
    first=mid1;
   else
    endn=mid1;
  }
  printf("%.6lf\n",endn);
    }
}
posted @ 2016-03-24 22:18  勿忘初心0924  阅读(254)  评论(0编辑  收藏  举报