일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- array.slice()
- 코딩공부
- node.js
- 코플릿
- 블록체인
- for문
- JavaScript
- 최강의 인생
- select
- Developer_JoyKim
- join
- 개발자_조이킴
- MySQL
- Where
- Programmers
- Hackerrank
- 재귀함수
- array
- SQL
- 코드스테이츠
- array.push()
- 정규표현식
- 자바스크립트
- 알고리즘
- 코딩테스트
- 개발자의 책장
- 배열
- Algorithms
- 역행자
- 프로그래머스
Archives
- Today
- Total
CodingSpace
[HackerRank/Algorithms] Implementation - Cats and a Mouse 본문
HackerRank/Algorithm
[HackerRank/Algorithms] Implementation - Cats and a Mouse
개발자_조이킴 2022. 8. 30. 21:00Problem. Implementation - Cats and a Mouse
Link.
https://www.hackerrank.com/challenges/cats-and-a-mouse/problem?isFullScreen=true
Cats and a Mouse | HackerRank
Which cat will catch the mouse first?
www.hackerrank.com
Description.
Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse does not move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight.
You are given q queries in the form of x,y, and z representing the respective positions for cats A and B, and for mouse C. Complete the function catAndMouse to return the appropriate answer to each query, which will be printed on a new line.
- If cat A catches the mouse first, print Cat A.
- If cat B catches the mouse first, print Cat B.
- If both cats reach the mouse at the same time, print Mouse C as the two cats fight and mouse escapes.
Key Point.
My Answer.
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the catAndMouse function below.
function catAndMouse(x, y, z) {
if(Math.abs(x-z) === Math.abs(y-z))
return "Mouse C";
else if(Math.abs(x-z) < Math.abs(y-z))
return "Cat A";
else
return "Cat B";
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const q = parseInt(readLine(), 10);
for (let qItr = 0; qItr < q; qItr++) {
const xyz = readLine().split(' ');
const x = parseInt(xyz[0], 10);
const y = parseInt(xyz[1], 10);
const z = parseInt(xyz[2], 10);
let result = catAndMouse(x, y, z);
ws.write(result + "\n");
}
ws.end();
}
References.
'HackerRank > Algorithm' 카테고리의 다른 글
[HackerRank/Algorithms] Warmup - Staircase (0) | 2022.09.03 |
---|---|
[HackerRank/Algorithms] Implementation - Repeated String (0) | 2022.09.01 |
[HackerRank/Algorithms] Implementation - Number Line Jumps (0) | 2022.08.29 |
[HackerRank/Algorithms] Warmup - Grading Students (0) | 2022.08.24 |
[HackerRank/Algorithms] Warmup - Time Conversion (0) | 2022.08.23 |
Comments