您的位置:首页 > 运维架构

HDU 2298 Toxophily

2015-07-20 00:34 393 查看
题意:人的坐标在原点,给出目标坐标(x,y) 和 原点的速度v,0 ≤ x, y, v ≤ 10000.问v和x的角度应该是多少,才能使得从思路原点发射的弓箭射中目标点。

思路:单峰的,可以三分,然而二元一次方程高中都学过,为什么不直接解出答案呢。

v* cosa* t = x; ==》t = x / v /cosa;

v* sina* t- 1/2* g * t * t = y;

消去t;x* tana - 1/2 * g x^2 (1 / v^2 / cosa^2) = y;

利用 sina^2 + cosa^2 = 1;把上面的式子变成

2* v^2 * tana - g * x^2 (1 + tana^2) = 2 v^2 *y;

这就变成了以tana为变量的二元一次方程。公式求一求就OK。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double g = 9.8;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double x,y,v;
scanf("%lf%lf%lf",&x,&y,&v);
double a = g * x * x;
double b = -2 * x * v * v;
double c = g * x * x + 2 * v * v * y;
double ok = b * b - 4 * a * c;
if(ok < 0)
{
printf("-1\n");
continue;
}
double x1 = atan((-b + sqrt(ok)) / (2 * a));
double x2 = atan((-b - sqrt(ok)) / (2 * a));
double ans = min(x1,x2);
if(ans < 0) printf("-1\n");
else printf("%6f\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  暴力