题意:给出一个物体的抛出的高度和初速度,求最远落点的位置。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5144
思路:高中物理,通过求抛出角与水平距离之间的关系,证明可得为二次函数,三分搜索求峰值。
注意点:物理公式推错,一不小心把初速度当成y轴方向上的速度分量,查了一个小时。
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
12515704 | 2014-12-16 00:32:12 | Accepted | 5144 | 436MS | 1252K | 1884 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 0.000000001;
const double g = 9.8;
double max ( double a, double b, double c )
{
a = max ( a, b );
a = max ( a, c );
return a;
}
double max ( double a, double b )
{
return a > b ? a : b;
}
double calc ( int h, int v, double angle )
{
double vy = sin ( angle ) * v;
double vx = cos ( angle ) * v;
double t = ( vy + sqrt ( 2 * g * h + vy * vy ) ) / g;
double dis = t * vx;
return dis;
}
double solve ( int h, int v )
{
double l, r;
l = 0.0 / 180.0 * pi;
r = 90.0 / 180.0 * pi;
double ans = 0;
while ( r - l > eps )
{
double first = calc ( h, v, ( 2 * l + r ) / 3 );
double second = calc ( h, v, ( l + 2 * r ) / 3 );
ans = max ( ans, first, second );
if ( first > second )
{
r = ( l + 2 * r ) / 3;
}
else
{
l = ( 2 * l + r ) / 3;
}
}
return ans;
}
int main()
{
ios::sync_with_stdio( false );
int h, v;
int ncase;
cin >> ncase;
while ( ncase -- )
{
cin >> h >> v;
cout << fixed << setprecision(2) << solve( h, v ) << endl;
}
return 0;
}