NPY and shot HDU - 5144(物理+三分)
题目来源: NPY and shot
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(T≤10000),which indicates the number of test cases.
The next T lines,each contains 2 integers H(0≤h≤10000m),which means the height of NPY,and v0(0≤v0≤10000m/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
题意
一个人站在高度为h的的地方以初速度v0向前斜抛,求水平位移的最大值
思路
利用高中物理可求得水平位移x=sqrt(v02-vy2)(vy+sqrt(vy^2+2gh))/g, vy是竖直方向上的初速度。
vy的范围是[0, v0],问题转化为在区间[0, v0]上求方程的最大值,三分。
输出保留两位小数
代码
#include<bits/stdc++.h>
using namespace std;
int h, v;
double f(double x)
{
double k1 = sqrt(v*v - x * x);
double k2 = x + sqrt(x*x + 2 * 9.8*h);
return k1 * k2 / 9.8;
}
double solve()
{
double l = 0, r = v, mid1, mid2;
for (int i = 1; i <= 1000; ++i)
{
mid1 = (l + r) / 2;
mid2 = (mid1 + r) / 2;
if (f(mid1) < f(mid2))
l = mid1;
else
r = mid2;
}
return f(l);
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> h >> v;
cout << setiosflags(ios::fixed) << setprecision(2) << solve() << endl;
}
return 0;
}