题目:http://acm.hdu.edu.cn/showproblem.php?pid=2298
竟然是物理题,初高中知识忘得差不多了,给出初速度,目标x,y位置,让你求射中该目标的角度.
三分+二分.斜抛的话,Y是先增大后减小的,三分求得斜抛的最大Y,若Y小于目标位置,则不可能射中.否则以此Y为上限进行二分.
嗯,好像也可以直接二分.
#include <iostream> #include <cmath> using namespace std; double x,y,v; const double g=9.8; const double PI=acos(-1.0); double cal(double a) { double y = x * tan(a) - g * x * x / 2 / ( v * v * cos(a) * cos(a)); return y; } double twodivide(double low,double heigh) { double mid; while(heigh-low>1e-9) { mid = (low + heigh)/2; if(cal(mid)>=y) heigh = mid; else low = mid; } return mid; } double threedivide(double low,double heigh) { double left = low; double right = heigh; double mid; double midmid; while(right-left>1e-9) { mid = (left + right)/2; midmid = (mid + right)/2; if(cal(mid)>=cal(midmid)) right = midmid; else left = mid; } return mid; } int main(int argc, const char *argv[]) { int T; //freopen("input.txt","r",stdin); cin>>T; while(T--) { cin>>x>>y>>v; double maxy = threedivide(0,PI/2); if(cal(maxy)<y) { printf("-1 "); }else { printf("%.6lf ",twodivide(0,maxy)); } } return 0; }