枚举角度
2 10 10 15 30 35 10.0 20.0 2 10 35 40 2 30 10.0 20.0 0
1 0HintIn the first case one of the best choices is that shoot the cannonballs parallelly to the horizontal line, then the first cannonball lands on 14.3 and the second lands on 28.6. In the second there is no shoot angle to make any cannonball land between [35,40] on the condition that no cannonball lands between [2,30].
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double pi=3.1415926,g=9.8; int n,ans; double h,l1,r1,l2,r2; double v[300]; int check(double angle) { int temp=0; for(int i=0;i<n&&temp>=0;i++) { double A=0.5*g; double B=-sin(angle)*v[i]; double C=-h; double time=-B+sqrt(B*B-4*A*C); time=time/(2.*A); double L=time*cos(angle)*v[i]; if( l2<=L && L<=r2 ) { temp=-1; break; } if( l1<=L && L<=r1 ) temp++; } return temp; } int main() { while(scanf("%d",&n)!=EOF&&n) { ans=0; scanf("%lf%lf%lf%lf%lf",&h,&l1,&r1,&l2,&r2); for(int i=0;i<n;i++) scanf("%lf",v+i); double dd=pi/1000.; for(double i=-pi/2;i<=pi/2;i+=dd) { ans=max(ans,check(i)); } printf("%d\n",ans); } return 0; }
原文:http://blog.csdn.net/ck_boss/article/details/39160693