Problem Statement
George wanted to arrange a race for his snail farm. He put all his snails on the starting line, shouted "GO!", and started watching the exciting race.
After an hour or two, he realized that snails are good tacticians. All of them were running at the same constant speed of snailSpeed meters per hour, probably saving strength for a rapid finish in the last meter. With distance meters remaining until the finish line, he decides to speed up the race and help his little friends by carrying each of them closer to the end. He needs to be careful, so he decides to carry only one snail at a time. George can walk georgeSpeed meters per hour. To keep the race fair, all the snails need to be moved by the same distance, and of course, all of them need to be transported before any of them reaches one meter from the finish line.
You are given four ints: snails, the number of snails, distance, the distance in meters remaining until the finish line, snailSpeed, the speed of each snail in meters per hour, and georgeSpeed, George's walking speed in meters per hour. Compute the shortest possible time (in hours) after which all the snails will be one meter from the finish line.
Definition
- Class:
- SnailRace
- Method:
- shortestTime
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double shortestTime(int snails, int distance, int snailSpeed, int georgeSpeed)
- (be sure your method is public)
Notes
- You can assume that lifting up and putting down a snail doesn't take any time.
- George can start carrying the first snail immediately because he is currently standing exactly where his snails are.
- After carrying each snail, George needs to go back to get the next one. He does so at the same speed (georgeSpeed meters per hour).
- Returned value must be within 1.0e-9 absolute or relative error.
Constraints
- snails will be between 1 and 50, inclusive.
- distance will be between 2 and 1000, inclusive.
- snailSpeed will be between 1 and 100, inclusive.
- georgeSpeed will be between 1 and 10000, inclusive.
Examples
2
11
4
8
Returns: 1.75
The snails have 10 meters to go before reaching the final meter. It takes George 0.75 hours to move the first snail 6 meters forward. At that time, the second snail will be at the 3rd meter. George immediately turns back and meets that snail at the 4th meter 0.25 hours later. Finally, it takes him 0.75 hours to carry the second snail to the 10th meter, where the first snail is, for a total of 1.75 hours.
3
12
2
100
Returns: 0.502
In this case, we have 3 snails, and each will be carried 10.2 meters and run the remaining 0.8 meters on its own. The total time is 0.102 + 0.4 = 0.502 hours.
2
11
50
40
Returns: 0.2
It is possible for a man to move slower than a snail! George can't help his snails, so he must wait 0.2 hours, letting the snails run on their own.
10
6
1
1000
Returns: 0.09323356231599604
2
201
10
20
Returns: 14.0
2
21
2
2
Returns: 10.0
2
21
3
2
Returns: 6.666666666666667
2
11
50
40
Returns: 0.2
10
6
1
1000
Returns: 0.09323356231599604
1
2
1
10000
Returns: 1.0E-4
50
2
50
10000
Returns: 0.006622408026755859
50
1000
1
2
Returns: 984.1633663366333
50
1000
1
1
Returns: 999.0
25
666
50
50
Returns: 13.3
49
123
20
600
Returns: 4.66065616797899
49
123
20
600
Returns: 4.66065616797899
50
1000
50
1000
Returns: 16.630411764705876
1
2
1
1
Returns: 1.0
2
2
1
1
Returns: 1.0
50
1000
50
51
Returns: 19.97208793535277
30
10
50
49
Returns: 0.18
7
12
33
133
Returns: 0.25930484574425383
50
2
100
10000
Returns: 0.004975376884422097
1
1000
100
10000
Returns: 0.0999
10
1000
1
10000
Returns: 1.8945104202016168
10
1000
100
1000
Returns: 6.579620689655172
45
321
58
9999
Returns: 1.878626488069296
40
12
10
12
Returns: 1.0949709060681474
33
999
9
1234
Returns: 35.66645341849004
7
777
7
777
Returns: 11.6301739527546
37
88
10
17
Returns: 8.57051736357191
45
2
100
1890
Returns: 0.008253281747257656
50
34
3
4678
Returns: 0.6566876709326888
17
678
56
57
Returns: 12.076704885573225
42
487
35
6501
Returns: 4.288817405968029
20
741
79
9359
Returns: 2.320437813010087
13
490
6
8146
Returns: 1.4736451514915478
32
845
62
492
Returns: 12.114224129962984
46
955
28
5437
Returns: 10.872750532685608
42
620
3
154
Returns: 127.51627404853215
43
396
22
8717
Returns: 3.1714363750452494
19
916
48
1727
Returns: 9.671833109878017
22
551
70
9913
Returns: 1.8303709501635825
18
327
36
9895
Returns: 1.0229660290060425
4
836
23
334
Returns: 11.924242424242424
24
670
42
7712
Returns: 3.2466078270558634
4
876
48
7645
Returns: 0.7681360464589337
13
791
38
2860
Returns: 5.186482021915091
24
752
30
779
Returns: 16.13792676769305
17
40
91
1843
Returns: 0.26597723167089504
39
138
41
8943
Returns: 0.8718700900006735
50
1000
1
2
Returns: 984.1633663366333
1
10
5
4
Returns: 1.8
50
1000
100
10000
Returns: 4.97040150753769
1
717
50
42
Returns: 14.32
1
100
5
6
Returns: 16.5