题意:给定一点(x,y)和射箭的初始速度v,求站在(0,0)点的Bob以怎样的角度射击才能击中目标,射击角度为箭与横坐标的夹角。
思路:画一个直角坐标系,建立如下函数 vx=v*cos(m); vy=v*sin(m); t=x/vx; fy=vy*t-9.8*t*t/2;,用二分法判断是否成立。
感悟:有时候数形结合很重要,我本来想建立一个二次函数,不过用三角函数个简单清晰。
AC代码:
#include<cmath>
#include<iomanip>
#include<algorithm>
using namespace std;
const double PI=3.1415926535497;
double x,y,v,s;
void f ()
{
double m,l,r,vx,vy,t,fy;
bool suc=0;
l=0; r=PI/2;
while ( r-l>0.0000000001)
{
m=(l+r)/2;
vx=v*cos(m);
vy=v*sin(m);
t=x/vx;
fy=vy*t-9.8*t*t/2;
if (fy<y)
l=m;
else
{
r=m;
s=m;suc=1;
}
}
if (suc)
cout<<fixed<<setprecision(6)<<s<<endl;
else cout<<"-1"<<endl;
}
int main()
{
int t;
cin>>t;
while (t--)
{
cin>>x>>y>>v;
f();
}
return 0;
}