题目链接:http://acm./showproblem.php?pid=5144
解题思路:BestCoder官方题解:
这是一个高一物理题。扔的距离与投掷角度的函数,符合单峰性质,所以三分角度就行了。当角度为θ时,设横向速度为vx=cos(θ)*v0,纵向速度为vy=sin(θ)*v0,则扔的距离为: (vy/g+sqrt((vy*vy/(g*2)+h)*2/g))*vx;这个应该手算一下,很容易就能推出来的,具体技巧是将速度延x,y轴分解,然后算一下球的滞空时间,然后乘以横向速度就行。
最初我采用的是直接暴力的方法,将cosθ的值从0.01到1一直遍历一遍,但是wa了,然后当时就没做出来。直到比赛结束,我很好奇的翻看那些大牛的代码,才知道,这题原来一步就可以求解,可能是当年的物理基础不扎实,才导致现在的自己吧。
一、公式推导:
AC代码:
#include<iostream>#include<cstdio>#include<cmath>using namespace std;const double g=9.8;int main(){int T;scanf("%d",&T);while(T--){double h,v;scanf("%lf%lf",&h,&v);printf("%.2lf\n",1.0*v*sqrt(1.0*v*v+2*g*h)/g);}return 0;}
二、三分
按bestcoder官方题解AC。
AC代码:
#include<iostream>#include<cstdio>#include<cmath>using namespace std;double h,v;double fun(double a){double g=9.8,vx=v*cos(a),vy=v*sin(a);return vx*(vy/g+sqrt((2*h*g+vy*vy)/(g*g)));}int main(){int T;scanf("%d",&T);while(T--){scanf("%lf%lf",&h,&v);double l=0,r=90.0;while((r-l)>=1e-10){double ll,rr;ll=(l*2+r)/3;rr=(l+2*r)/3;if(fun(ll)<fun(rr))l=ll;elser=rr;}printf("%.2lf\n",fun(l));}return 0;}