Advertisement
PrimarchVG

Tortoise racing

Jul 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.23 KB | None | 0 0
  1. //Two tortoises named A and B must run a race. A starts with an average speed of 720 feet per hour. Young B knows she runs faster than //A, and furthermore has not finished her cabbage.
  2.  
  3. //When she starts, at last, she can see that A has a 70 feet lead but B's speed is 850 feet per hour. How long will it take B to catch //A?
  4.  
  5. //More generally: given two speeds v1 (A's speed, integer > 0) and v2 (B's speed, integer > 0) and a lead g (integer > 0) how long will //it take B to catch A?
  6.  
  7. //The result will be an array [hour, min, sec] which is the time needed in hours, minutes and seconds (round down to the nearest //second) or a string in some languages.
  8.  
  9. //If v1 >= v2 then return nil, nothing, null, None or {-1, -1, -1} for C++, C, Go, Nim, [] for Kotlin or "-1 -1 -1".
  10.  
  11. #My_solution
  12.  
  13. fun race(v1:Int, v2:Int, g:Int):IntArray {
  14.     // your code
  15.    
  16.     if (v1 >= v2) return intArrayOf()
  17.    
  18.     val time : Int = g * 3600 / (v2 - v1)
  19.    
  20.     return intArrayOf(time / 3600, (time % 3600) / 60, time % 60)
  21. }
  22.  
  23. #Best_solution
  24. fun race(v1:Int, v2:Int, g:Int):IntArray {
  25.     return if (v1 >= v2) {
  26.         intArrayOf()
  27.     } else {
  28.         val s  = (3600 * g) / (v2 - v1)
  29.         intArrayOf( s/3600, s/60%60, s%60 )
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement