Toxophily-數論以及二分三分
cid=83980#status//G/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">
id=19904" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">
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.We all like toxophily.
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?
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.
Input
Technical Specification
1. T ≤ 100.
2. 0 ≤ x, y, v ≤ 10000.
Output
For each test case, output the smallest answer rounded to six fractional digits on a separated line.Output "-1", if there‘s no possible answer.
Please use radian as unit.
Sample Input
3 0.222018 23.901887 121.909183 39.096669 110.210922 20.270030 138.355025 2028.716904 25.079551
Sample Output
1.561582 -1 -1第一種是通過數學公式求解,另外一種是三分一次高度所相應的傾斜角,再二分符合條件的傾斜角 公式法:
有題目能夠知道:x,y,v都是已知條件
設vx=v*cos(α),vy=v*sin(α),同一時候從P(0,0)點到達目標點花了t時間,重力加速度為G=9.8.
∴x=vx*t,y=vy*t-1/2*G*t2.
消掉vx,vy,t能夠轉換為y=v*sin(α)*x/(v*cos(α))-1/2*g*x2/(v2*cos(α)2).
∴將sin(α)/cos(α)=tan(α);
∴y=v*x*tan(α)-(1/2*g*x2/v2)*((sin(α)2+cos(α)2)/cos(α)2);
∴y=v*x*tan(α)-(1/2*g*x2/v2)*(1+tan(α)2);
∴將其進行整理能夠得到:g*x2*tan(α)2-2*v2*x*tan(α)+2*v2y+g*x2=0;
∴能夠得到△=b2-4*a*c;
∴令a=g*x2,b=-2*v2*x,c=2*v2y+g*x2.
又∵x1=(-b+(b2-4*a*c)?)/(2*a),x2=(-b-(b2-4*a*c)?)/(2*a).
∴能夠通過上述公式將tan(α)求出,然後就是通過atan((tan(α)))將α求出
接著檢查α是否符合條件就能夠了。
/* Author: 2486 Memory: 1616 KB Time: 0 MS Language: C++ Result: Accepted */ #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double PI=acos(-1); const double G=9.8; int T; double x,y,v; int main() { scanf("%d",&T); while(T--) { scanf("%lf%lf%lf",&x,&y,&v); double a=G*x*x,b=-2.0*v*v*x,c=2.0*v*v*y+G*x*x; double posi=(-b+sqrt(b*b-4.0*a*c))/2.0/a; double ne=(-b-sqrt(b*b-4.0*a*c))/2.0/a; posi=atan(posi),ne=atan(ne); if(posi>=0&&posi<=PI/2.0&&ne>=0&&ne<=PI/2.0) { printf("%.6lf\n",posi>ne?ne:posi); } else if(ne>=0&&ne<=PI/2.0) { printf("%.6lf\n",ne); } else if(posi>=0&&posi<=PI/2.0) { printf("%.6lf\n",posi); } else printf("-1\n"); } return 0; }
三分二分方法
/* Author: 2486 Memory: 1628 KB Time: 0 MS Language: C++ Result: Accepted */ #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double PI=acos(-1); const double eps=1e-10; int T; double x,y,v; double C(double m) { double vx=v*cos(m),vy=v*sin(m); return (vy*x)/vx-9.8*(x/vx*x/vx)/2.0; } bool B(double m) { double vx=v*cos(m),vy=v*sin(m); return (vy*x)/vx-9.8*(x/vx*x/vx)/2.0>=y; } int main() { scanf("%d",&T); while(T--) { scanf("%lf%lf%lf",&x,&y,&v); double lb=0,ub=PI/2.0; ///////////////求出最大高度所相應的傾斜度//////////////// while(ub-lb>eps) { double mid=(ub+lb)/2.0; double mmid=(ub+mid)/2.0; if(C(mid)>C(mmid)) { ub=mmid; } else lb=mid; } if(C(ub)<y) { printf("-1\n"); continue; } /////////////////////////////// lb=0; ////////////////求出無限接近目標的傾斜度/////////////// while(ub-lb>eps) { double mid=(ub+lb)/2.0; if(B(mid)) { ub=mid; } else lb=mid; } /////////////////////////////// printf("%.6lf\n",ub); } return 0; }
Toxophily-數論以及二分三分