2016SDAU編程練習二1004

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.<br>We all like toxophily.<br><br>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?<br><br>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. <br>
 


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.<br>Technical Specification<br><br>1. T ≤ 100.<br>2. 0 ≤ x, y, v ≤ 10000. <br>
 


Output
For each test case, output the smallest answer rounded to six fractional digits on a separated line.<br>Output "-1", if there's no possible answer.<br><br>Please use radian as unit. <br>
 


Sample Input
3<br>0.222018 23.901887 121.909183<br>39.096669 110.210922 20.270030<br>138.355025 2028.716904 25.079551<br> 


Sample Output
1.561582<br>-1<br>-1<br> 


Source

The 4th Baidu Cup final


題意:已知發射點座標爲(0,0)和重力加速度g=9.8,給出目標的座標和初速度,求能夠擊中目標的最小仰角

思路:就是物理問題套公式,再用二分

感想:二分法主要在精度啊

AC代碼:

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
double PI = 3.1415926535897932384626433832795;
int T;
double x,y,v,ans;
void Search() {
    double mid,l,r,vx,vy,t,ang;
    l = 0;r = PI/2.0;
    bool d = false; 
    while(r-l>=0.000000001)
    {
        mid = (l+r)/2.0;
        vx = v*cos(mid),vy = v*sin(mid);t = x/vx;
        ang = vy*t-0.5*9.8*t*t;
        if(ang < y)
        {
            l = mid;
        }
        else if(ang > y || vy/9.8 < t)
        {
            d = true;
            ans = mid;
            r = mid;
        }
        else
        {
            d = true;
            ans = mid;
            break;
        }
    }
    if(d) printf("%.6lf\n",ans);
    else printf("-1\n");
}
int main () 
{
    for(scanf("%d",&T);T--;)
    {
        scanf("%lf %lf %lf",&x,&y,&v);
        Search();
    }
    return 0;
}

 
發佈了59 篇原創文章 · 獲贊 0 · 訪問量 7464
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章