首页 > 其他 > 详细

HDU 2298:Toxophily(推公式)

时间:2017-04-26 00:35:47      阅读:242      评论:0      收藏:0      [点我收藏+]

http://acm.hdu.edu.cn/showproblem.php?pid=2298

题意:给出一个x,y,v,问从(0,0)以v为初速度射箭,能否射到(x,y)这个点,如果能,输出最小的射出角度(与x轴),否则输出-1.

思路:首先考虑不能到达的情况,由动能定理mgy > 1 / 2 * m * v * v的时候,就输出-1.

然后可以列出两个式子:

x = v * t * cos(θ)  ①

y = v * t * sin(θ) - 1 / 2 * g * t * t. ②

把①带入②: y = x * tan(θ) - 1 / 2 * g * (x / v / cos(θ)) ^ 2.

y = x * tan(θ) - (g * x ^ 2) / (2 * v) * (1 + tan(θ)^2).

得到一元二次方程:

x * x * g * tan(θ)^2 - 2 * v * v * x + g * x * x + 2 * v * v * y = 0.

然后求解,注意角度合法的范围在0<=θ<=PI/2中间,然后取较小的一个。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const double eps = 1e-7;
 4 const double G = 9.8;
 5 const double PI = acos(-1.0) / 2;
 6 double x, y, v, t;
 7 
 8 int main() {
 9     int T; scanf("%d", &T);
10     while(T--) {
11         scanf("%lf%lf%lf", &x, &y, &v);
12         if(y > v * v / 2 / G) {
13             puts("-1");
14         } else {
15             double a = G * x * x;
16             double b = -2.0 * v * v * x;
17             double c = G * x * x + 2 * v * v * y;
18             double delta = b * b - 4 * a * c;
19             double x1 = (-b + sqrt(delta)) / 2 / a, x2 = (-b - sqrt(delta)) / 2 / a;
20             x1 = atan(x1), x2 = atan(x2);
21             int f1 = 0, f2 = 0;
22             if(0 <= x1 && x1 <= PI) f1 = 1;
23             if(0 <= x2 && x2 <= PI) f2 = 1;
24             if(!f1 && !f2) puts("-1");
25             else if(f1 && f2) printf("%.6f\n", x1 < x2 ? x1 : x2);
26             else if(f1) printf("%.6f\n", f1);
27             else printf("%.6f\n", f2);
28         }
29     }
30     return 0;
31 }

 

HDU 2298:Toxophily(推公式)

原文:http://www.cnblogs.com/fightfordream/p/6765306.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!