题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5144
题目意思:有个人抛物体,已知抛的速度和高度,问可以抛到的最远距离是多少。即水平距离。
做的时候是抄公式的,居然过了,幸运幸运............
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; const double g = 9.8; int main()
{
int T;
double h, v;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%lf%lf", &h, &v);
double tmp = sqrt(v*v + 1ll * * g * h);
double t = tmp / g;
double ans = t * v;
printf("%.2lf\n", ans);
}
}
return ;
}
乌冬子做的比较正规,是用三分做的,还有浮点精度控制,值得学习 ^_^ (未经他允许就盗他版权,应该不会怪我的......)
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <vector> using namespace std; const double EPS = 1e-;
const double PI = acos(-1.0);
const double G = 9.8; int h, v; double F(double ang)
{
double hv = cos(ang)*v;
double vv = sin(ang)*v;
double t1 = vv/G;
double t2 = sqrt(*(vv*t1-G*t1*t1/2.0+h)/G);
return hv*(t1+t2);
} int main()
{
int kase;
scanf("%d", &kase);
while (kase--) {
scanf("%d%d", &h, &v);
double l = 0.0, r = PI/;
while (l < r-EPS) {
double d = (r-l)/3.0;
double ll = l+d;
double rr = r-d;
if (F(ll) < F(rr))
l = ll;
else
r = rr;
}
printf("%.2f\n", F(l));
}
return ;
}