Problem Statement
We are on a planet with no atmosphere. The gravitational acceleration on the surface of this planet is g cm/s^2 = (g/100) m/s^2.
We have just dropped a bouncy ball from the height of h meters. Each time the ball reaches the ground, it bounces upwards again. The bounce happens in an instant. In each bounce the ball loses p percent of its current kinetic energy.
Calculate and return the height of the ball (in meters) after t seconds.
Definition
- Class:
- BouncingBall
- Method:
- getPosition
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double getPosition(int g, int h, int p, int t)
- (be sure your method is public)
Notes
- We assume that the gravitational acceleration is constant (i.e., it does not depend on the current height of the ball) and that its direction is straight down.
- Your return value must have an absolute or a relative error at most 1e-9.
- The constraints are such that after t seconds the ball is still bouncing. See Example #5.
Constraints
- g will be between 10 and 1000, inclusive.
- h will be between 10 and 1000, inclusive.
- p will be between 0 and 10, inclusive.
- t will be between 1 and 50, inclusive.
Examples
981
10
0
1
Returns: 5.095
We are on a planet whose gravitational acceleration constant is similar to our Earth: 9.81 m/s^2. We dropped an ideal bouncing ball from the height of 10 meters. After one second the ball is still falling: it is now 5.095 meters above ground and its speed is 9.81 m/s.
981
10
0
2
Returns: 6.408564143658009
The same situation as above, but now we want the situation after two seconds. Approximately 1.427843 seconds after the process started the ball hits the ground for the first time and bounces upwards again. As no energy got lost, the ball's speed immediately after the bounce is the same as its speed immediately before the bounce, only the direction changes from downwards to upwards. This speed is approximately 14.007141 m/s. The ball then travels upwards for approximately another 0.572157 seconds. After those seconds the ball is now approximately 6.408564 meters up in the air and its current speed upwards is 8.394282 m/s.
981
10
0
47
Returns: 1.5951180154118365
Still the same situation as above, now we want the height after 47 seconds. The ball keeps bouncing. Once every approximately 2.855686 seconds the ball hits the ground. During each bounce the maximum height the ball reaches is exactly h, and it happens exactly in the middle of the bounce. The last bounce before 47 seconds elapse will happen at roughly 44.263137 seconds. The first bounce after 47 seconds will happen at roughly 47.118823 seconds. Thus, at 47 seconds the ball is already on its way down and pretty close to hitting the ground again.
981
10
8
2
Returns: 6.081311391188509
Still the same planet, but now we used a less bouncy ball. This one loses 8% of its energy each time it bounces. This means that the maximum height of each bounce will be 8% lower than the maximum height of the previous bounce. The first 1.427843 seconds happen just like in all the examples above. However, once the ball bounces off the ground for the first time, its speed upwards will now be only 13.435178 m/s (instead of the 14.007141 m/s we saw in Example #1) and therefore the height of the ball after two seconds is slightly lower than in Example #1. (Please note that the speed immediately after the bounce is not 8% lower than the speed immediately before the bounce. The ball loses 8 percent of its energy, not speed.)
981
10
8
47
Returns: 0.7952075484286876
A general case: the same scenario as Example #3 but many bounces later.
1000
10
10
50
Returns: 0.036079462357220954
The test case with the most bounces possible. Note that the constraints imply that after t seconds the ball is still bouncing.
10
1000
7
50
Returns: 875.0
600
867
0
17
Returns: 0.0
600
507
0
39
Returns: 0.0
370
74
0
28
Returns: 60.495724747552806
576
474
1
37
Returns: 94.56146883937583
361
273
2
33
Returns: 136.2947136550347
405
411
3
40
Returns: 120.40954710114994
51
824
4
27
Returns: 638.105
684
34
5
7
Returns: 30.252214340829525
10
280
6
31
Returns: 231.95
751
569
7
1
Returns: 565.245
949
845
8
21
Returns: 651.8158468379727
591
447
9
15
Returns: 165.7212666242137
253
545
10
33
Returns: 420.331945050674
377
462
0
16
Returns: 20.112422629186312
44
584
1
24
Returns: 457.28
743
714
2
23
Returns: 621.5385338526279
625
360
3
36
Returns: 215.24080555280568
598
790
4
13
Returns: 284.68999999999994
254
817
5
47
Returns: 764.064847616695
946
967
6
3
Returns: 924.43
138
351
7
7
Returns: 317.19
381
602
8
25
Returns: 369.8562923140215
530
563
9
17
Returns: 163.07617663706674
109
674
10
15
Returns: 551.375
959
725
0
22
Returns: 692.7697010243623
233
492
1
48
Returns: 429.95788237244983
122
801
2
27
Returns: 356.31000000000006
629
371
3
44
Returns: 344.4733422441822
903
303
4
50
Returns: 243.75918594356395
839
254
5
36
Returns: 94.94021167152323
954
277
6
23
Returns: 39.46946483507563
555
588
7
3
Returns: 563.025
505
329
8
37
Returns: 161.23890004929834
808
414
9
17
Returns: 345.5231494385763
977
757
10
34
Returns: 217.69167882359238
828
236
0
39
Returns: 71.63508303525782
126
652
1
14
Returns: 528.52
398
507
2
17
Returns: 63.15580110076087
995
628
3
36
Returns: 251.0933631543494
162
464
4
1
Returns: 463.19
184
570
5
41
Returns: 480.36313206883335
833
789
6
6
Returns: 639.06
86
282
7
33
Returns: 133.48852488536255
842
647
8
35
Returns: 112.10815684740754
497
811
9
28
Returns: 605.6327922843059
384
950
10
49
Returns: 793.6294058950458
43
632
0
17
Returns: 569.865
930
669
1
39
Returns: 300.6230525996766
539
976
2
44
Returns: 855.1879905316963
941
493
3
46
Returns: 312.53605549718304
663
827
4
21
Returns: 444.26004442033695
859
32
5
37
Returns: 4.856132711361038