문제 설명
캥거루 2마리가 뛰는데 캥거루1은 캥거루2보다 항상 더 미리 뛴다
캥거루1이랑 캥거루2의 위치가 같아지면 yes를 리턴
그럴 수 없으면 no를 리턴하는 문제
캥거루들은 x에서 시작해서 v만큼 뛴다
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'kangaroo' function below.
*
* The function is expected to return a STRING.
* The function accepts following parameters:
* 1. INTEGER x1
* 2. INTEGER v1
* 3. INTEGER x2
* 4. INTEGER v2
*/
function kangaroo(x1, v1, x2, v2) {
// Write your code here
let kang1 = x1
let kang2 = x2
for (let i=0; i<10000; i++) {
kang1 += v1
kang2 += v2
if(kang1 === kang2) return "YES"
}
return "NO"
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
const x1 = parseInt(firstMultipleInput[0], 10);
const v1 = parseInt(firstMultipleInput[1], 10);
const x2 = parseInt(firstMultipleInput[2], 10);
const v2 = parseInt(firstMultipleInput[3], 10);
const result = kangaroo(x1, v1, x2, v2);
ws.write(result + '\n');
ws.end();
}
캥거루1과 캥거루2가 시작하는 지점을 변수로 선언해주고
반복문으로 뛰는 만큼(v만큼) 계속 더해준 뒤
같아지면 YES를 리턴, 계쏙 더해도 같아지지 않으면 NO를 리턴!
해결