Problem
- Two tortoises named A and B must run a race.
- A starts with an average speed of 720 feet per hour.
- A는 시속 720 feet의 평균 속도로 시작한다.
- B know she runs faster than A, and furthemore has not finished her cabbage.
- B는 자신이 A보다 빠르다는 것을 알고 있다.
- When she starts, at last, she can see that A has a 70 feet lead but B's speed is 850 feet per hour.
- A가 70 feet를 앞서고 있지만, B는 시속 850 feet이다.
- How long will it take B to catch A?
- Given two speeds ‘v1’(A's speed, integer > 0) and ‘v2’(B's speed, integer > 0) and a lead ‘g’(integer > 0).
- 두 가지 속도 ‘v1’과 ‘v2’, 앞서는 값 ‘g’가 주어진다.
- The result will be an array ‘[hour, min, sec]’ which is the time needed in hours, minutes and seconds or a string in some languages.
- If ‘v1 >= v2’ then return ‘null’.
- ‘v1 >= v2’라면 ‘null’을 반환한다.
Solution 01
function tortoiseRacing(v1, v2, g) {
if (v1 >= v2) return null;
const result = Math.floor((g / (v2 - v1)) * 3600);
const hour = Math.floor(result / 3600);
const min = Math.floor((result - hour * 3600) / 60);
const sec = result - hour * 3600 - min * 60;
return [hour, min, sec];
}
tortoiseRacing(720, 850, 70); // [0, 32, 18]
tortoiseRacing(80, 91, 37); // [3, 21, 49]
tortoiseRacing(80, 100, 40); // [2, 0, 0]
Solution 02
function tortoiseRacing(v1, v2, g) {
if (v1 >= v2) return null;
const result = (g * 3600) / (v2 - v1);
const hour = Math.floor(result / 3600);
const min = Math.floor((result % 3600) / 60);
const sec = Math.floor((result % 3600) % 60);
return [hour, min, sec];
}
tortoiseRacing(720, 850, 70); // [0, 32, 18]
tortoiseRacing(80, 91, 37); // [3, 21, 49]
tortoiseRacing(80, 100, 40); // [2, 0, 0]
Solution 03
function tortoiseRacing(v1, v2, g) {
const result = Math.floor((g / (v2 - v1)) * 3600);
return v1 >= v2 ? null : [Math.floor(result / 3600), Math.floor((result % 3600) / 60), result % 60];
}
tortoiseRacing(720, 850, 70); // [0, 32, 18]
tortoiseRacing(80, 91, 37); // [3, 21, 49]
tortoiseRacing(80, 100, 40); // [2, 0, 0]