题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5144
题目意思:有个人抛物体,已知抛的速度和高度,问可以抛到的最远距离是多少。即水平距离。
做的时候是抄公式的,居然过了,幸运幸运............
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const double g = 9.8; 9 10 int main() 11 { 12 int T; 13 double h, v; 14 while (scanf("%d", &T) != EOF) 15 { 16 while (T--) 17 { 18 scanf("%lf%lf", &h, &v); 19 double tmp = sqrt(v*v + 1ll * 2 * g * h); 20 double t = tmp / g; 21 double ans = t * v; 22 printf("%.2lf\n", ans); 23 } 24 } 25 return 0; 26 }
乌冬子做的比较正规,是用三分做的,还有浮点精度控制,值得学习 ^_^ (未经他允许就盗他版权,应该不会怪我的......)
1 #include <algorithm> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cmath> 5 #include <iostream> 6 #include <map> 7 #include <numeric> 8 #include <vector> 9 10 using namespace std; 11 12 const double EPS = 1e-8; 13 const double PI = acos(-1.0); 14 const double G = 9.8; 15 16 int h, v; 17 18 double F(double ang) 19 { 20 double hv = cos(ang)*v; 21 double vv = sin(ang)*v; 22 double t1 = vv/G; 23 double t2 = sqrt(2*(vv*t1-G*t1*t1/2.0+h)/G); 24 return hv*(t1+t2); 25 } 26 27 int main() 28 { 29 int kase; 30 scanf("%d", &kase); 31 while (kase--) { 32 scanf("%d%d", &h, &v); 33 double l = 0.0, r = PI/2; 34 while (l < r-EPS) { 35 double d = (r-l)/3.0; 36 double ll = l+d; 37 double rr = r-d; 38 if (F(ll) < F(rr)) 39 l = ll; 40 else 41 r = rr; 42 } 43 printf("%.2f\n", F(l)); 44 } 45 return 0; 46 }