Pinball
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
Problem Description
There is a slope on the 2D plane. The lowest point of the slope is at the origin. There is a small ball falling down above the slope. Your task is to find how many times the ball has been bounced on the slope.
It’s guarantee that the ball will not reach the slope or ground or Y-axis with a distance of less than 1 from the origin. And the ball is elastic collision without energy loss. Gravity acceleration g = 9.8 m / s 2 g=9.8m/s^2 g=9.8m/s2.
Input
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 100), indicating the number of test cases.
The first line of each test case contains four integers a, b, x, y (1 ≤ a, b, -x, y ≤ 100), indicate that the slope will pass through the point(-a, b), the initial position of the ball is (x, y).
Output
Output the answer.
It’s guarantee that the answer will not exceed 50.
Sample Input
1
5 1 -5 3
Sample Output
2
Tips
题意:
如图所示,一个小球(可看作质点)从
(
x
,
y
)
(x,y)
(x,y) 除由静止自由下落,下方有一以原点为顶点,过点
(
−
a
,
b
)
(-a,b)
(−a,b) 的斜面。问小球在落地(落到
x
x
x 轴上)之前共弹了几次。
题解:
如下图所示,沿斜面建系,将重力加速度分解到与斜面平行的方向和与斜面垂直的方向,分别记作
g
p
=
g
sin
θ
,
g
v
=
g
cos
θ
g_p=g\sin\theta,g_v=g\cos\theta
gp=gsinθ,gv=gcosθ ,其中
θ
=
arctan
b
a
\theta=\arctan\frac{b}{a}
θ=arctanab 是斜面的倾角。则小球在斜面上的运动就可以分解为沿着斜面方向的匀加速运动和沿着与斜面垂直方向的匀加速运动。计算出沿斜面方向运动结束的时间
t
p
t_p
tp 和沿着与斜面垂直方向的一次下落时间
t
v
t_v
tv ,则弹跳的次数即为
1
+
⌊
t
p
−
t
v
2
t
v
⌋
1+\left\lfloor\frac{t_p-t_v}{2t_v}\right\rfloor
1+⌊2tvtp−tv⌋ 由于第一次下落是一半的周期,而之后都是起-落的循环,因此要先用总时间减去第一次下落的时间算出接下来弹跳的次数,再加上第一次的。
至于如何计算两个时间,首先来说, t v t_v tv 很容易计算,先计算 h = ( y + x tan θ ) cos θ = y cos θ + x sin θ h=(y+x\tan\theta)\cos\theta=y\cos\theta+x\sin\theta h=(y+xtanθ)cosθ=ycosθ+xsinθ ,然后用 h = 1 2 g v t v 2 h=\frac{1}{2}g_vt_v^2 h=21gvtv2 即可得出 t v = 2 ( y cos θ + x sin θ ) g cos θ t_v=\sqrt{\frac{2(y\cos\theta+x\sin\theta)}{g\cos\theta}} tv=gcosθ2(ycosθ+xsinθ)
而 t p t_p tp 则与之相仿,首先计算 s = − x + ( y + x tan θ ) sin θ = y sin θ − x cos θ s=-x+(y+x\tan\theta)\sin\theta=y\sin\theta-x\cos\theta s=−x+(y+xtanθ)sinθ=ysinθ−xcosθ ,可得 t p = 2 ( y sin θ − x cos θ ) g sin θ t_p=\sqrt{\frac{2(y\sin\theta-x\cos\theta)}{g\sin\theta}} tp=gsinθ2(ysinθ−xcosθ)
至此本题结束。
Reference Code
#include <cstdio>
#include <cmath>
const double g=9.8;
int T,a,b,x,y;
int solve(){
double k=-1.0*b/a;
double t=atan(-k);
double gp=g*sin(t);
double gv=g*cos(t);
double tp=sqrt(2*(y*sin(t)-x*cos(t))/gp);
double tv=sqrt(2*(y*cos(t)+x*sin(t))/gv);
return 1+(int)((tp-tv)/(2*tv));
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d%d%d%d",&a,&b,&x,&y);
printf("%d\n",solve());
}
return 0;
}