hdu 5144 NPY and shot(物理+三分法)

NPY and shot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 807    Accepted Submission(s): 341


Problem Description
NPY is going to have a PE test.One of the test subjects is throwing the shot.The height of NPY is H meters.He can throw the shot at the speed of v0 m/s and at the height of exactly H meters.He wonders if he throws the shot at the best angle,how far can he throw ?(The acceleration of gravity, g, is  9.8m/s2 )
 

Input
The first line contains a integer  T(T10000) ,which indicates the number of test cases.
The next T lines,each contains 2 integers  H(0h10000m) ,which means the height of NPY,and  v0(0v010000m/s) , which means the initial velocity.
 

Output
For each query,print a real number X that was rounded to 2 digits after decimal point in a separate line.X indicates the farthest distance he can throw.
 

Sample Input
  
  
2 0 1 1 2
 

Sample Output
  
  
0.10 0.99
Hint
If the height of NPY is 0,and he throws the shot at the 45° angle, he can throw farthest.
题意:一个人高为h,以vo为初速度从任意角度抛出一颗球,问球最远能到达多远

思路:易知随着角度递增,水平位移会有一个单峰极值,所以我们可以直接带进三分法里面取求解最大值、

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
double v,h,t1,t2;
const double g = 9.8;
const double pi = 3.1415926;
const double eps = 1e-8;
double Calc(double t)
{
    t1 = sin(t)*v/g;
    t2 = sqrt((h+v*sin(t)*t1-0.5*g*t1*t1)*2/g);
    return (t1+t2)*v*cos(t);
}
double solve(double MIN,double MAX)
{
    double Left, Right;
    double mid, midmid;
    double mid_value, midmid_value;
    Left = MIN;
    Right = MAX;
    while (Left +eps < Right)
    {
        mid = (Left + Right) / 2;
        midmid = (mid + Right) / 2;
        mid_value = Calc(mid);
        midmid_value = Calc(midmid);
        if (mid_value >= midmid_value) Right = midmid;
        else Left = mid;
    }
    return Left;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%lf%lf",&h,&v);
        double angle = solve(0,pi/2);
        printf("%.2lf\n",Calc(angle));
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值