Statistics

Problem Statement for "HareNap"

Problem Statement

The Hare wants a rematch with the Tortoise. In his arrogance, the Hare will still take a nap during the race, but this time he will be smart: he will rest for a time period such that be can beat the Tortoise by a pre-determined amount of time.

Create a class HareNap that contains the method napTime, which takes the speed of the Hare in meters per second, the speed of the Tortoise in meters per second, the length of the race in meters, and the time that the Hare wants to win the race by in seconds, and returns the time that the Hare can sleep during the race in seconds, rounded to the nearest whole second.

Definition

Class:
HareNap
Method:
napTime
Parameters:
int, int, int, int
Returns:
int
Method signature:
int napTime(int hareSpeed, int tortoiseSpeed, int raceLength, int winTime)
(be sure your method is public)

Notes

  • If the Hare cannot possibly beat the tortoise by winTime seconds, return 0.
  • If the Hare has no time to sleep during the race, return 0.
  • Round your answer to the nearest whole second. 0.5 rounds up to 1, while 0.4 rounds down to 0.

Constraints

  • hareSpeed and tortoiseSpeed are between 1 and 100, inclusive.
  • raceLength is between 1 and 1000, inclusive.
  • winTime is between 0 and 30, inclusive.

Examples

  1. 15

    1

    30

    5

    Returns: 23

    The Tortoise will finish the race in 30 seconds. Without napping, the Hare can finish the race in 2 seconds. The Hare can sleep for 23 seconds and still win the race by 5 seconds.

  2. 84

    35

    750

    8

    Returns: 5

    The Tortoise will finish the race in about 21.43 seconds. Without napping, the Hare can finish the race in about 8.93 seconds. The Hare can sleep for exactly 4.5 seconds and still beat the Tortoise by 8 seconds.

  3. 60

    10

    127

    11

    Returns: 0

    The Tortoise will finish the race in 12.7 seconds. Without napping, the Hare can finish the race in about 2.12 seconds. The Hare cannot beat the Tortoise by 11 seconds even if he doesn't sleep.

  4. 80

    40

    800

    10

    Returns: 0

    The Tortoise will finish the race in 20 seconds. Without napping, the Hare can finish the race in 10 seconds. The Hare can win the race by 10 seconds, but has no time to nap.

  5. 90

    40

    468

    6

    Returns: 1

    The Tortoise will finish the race in 11.7 seconds. Without napping, the Hare can finish the race in 5.2 seconds. The Hare can sleep for 0.5 seconds and still beat the Tortoise by 6 seconds.

  6. 49

    37

    982

    0

    Returns: 6

  7. 15

    1

    30

    5

    Returns: 23

  8. 25

    35

    650

    3

    Returns: 0

  9. 30

    30

    120

    1

    Returns: 0

  10. 20

    30

    600

    0

    Returns: 0

  11. 35

    25

    650

    3

    Returns: 4

  12. 100

    100

    1000

    1

    Returns: 0

  13. 100

    99

    1000

    0

    Returns: 0

  14. 100

    91

    1000

    0

    Returns: 1

  15. 100

    40

    1000

    13

    Returns: 2

  16. 60

    10

    125

    10

    Returns: 0

  17. 60

    10

    127

    11

    Returns: 0

  18. 90

    80

    40

    0

    Returns: 0

  19. 75

    25

    50

    0

    Returns: 1

  20. 25

    75

    50

    0

    Returns: 0

  21. 80

    40

    800

    10

    Returns: 0

  22. 84

    35

    750

    8

    Returns: 5

  23. 90

    40

    468

    6

    Returns: 1

  24. 51

    24

    340

    3

    Returns: 5

  25. 99

    91

    563

    0

    Returns: 0

  26. 97

    87

    422

    0

    Returns: 1

  27. 97

    75

    496

    0

    Returns: 1

  28. 87

    49

    617

    0

    Returns: 5

  29. 97

    61

    904

    0

    Returns: 6

  30. 35

    11

    8

    0

    Returns: 0

  31. 49

    37

    982

    0

    Returns: 6

  32. 10

    60

    60

    10

    Returns: 0

  33. 25

    50

    500

    5

    Returns: 0

  34. 1

    1

    1

    30

    Returns: 0

  35. 1

    10

    300

    0

    Returns: 0

  36. 15

    1

    30

    30

    Returns: 0

  37. 49

    80

    982

    0

    Returns: 0

  38. 25

    20

    4

    30

    Returns: 0

  39. 12

    10

    60

    5

    Returns: 0

  40. 10

    9

    20

    5

    Returns: 0

  41. 15

    1

    30

    2

    Returns: UNKNOWN-OUTPUT10291821323


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: