Algorithm/JavaScript(Node.js)
백준 2869번: 달팽이는 올라가고 싶다(The snail wants to go up) Node.js(JavaScript)
감공사
2020. 1. 15. 23:54
728x90
Code
😢 이해하고 식을 만들어 내는 것이 조금 힘들었다. 미끄러지는 부분을 정확히 이해해야 한다.
😊 높이 V를 A-B로 나눠주는 방법이 가장 효율적이다.
하지만, 정상에 도착하면 미끄러지지 않기 때문에 B가 결국 한 번 더 계산식에 들어가는 셈이다.
따라서, 높이에V에 B를 빼주면 위 문제를 해결할 수 있다.
딱 떨어지지 않는 수가 나올 때는, 하루가 더 필요하다는 뜻이기 때문에 Math.ceil로 올림을 해주면 같은 효과를 볼 수 있다.
Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)
// The snail wants to go up. |
// 1st Solution - Good |
// For submit |
// const fs = require('fs'); |
// const input = fs.readFileSync('/dev/stdin').toString().trim().split(' ').map(num => parseInt(num)); |
// For local test |
const input = [2, 1, 5]; // 4 |
// const input = [10, 3, 54]; // 정답은 8 인데 7.285714가 나온다(Math.ceil()없을 때) |
// const input = [5, 1, 11]; // 정답은 3인데 2.5가 나온다(Math.ceil()없을 때) |
const A = input.shift(); |
const B = input.shift(); |
const V = input.shift(); |
console.log(Math.ceil((V - B) / (A - B))); |
// 2nd Solution - Good |
// For submit |
// const fs = require('fs'); |
// const input = fs.readFileSync('/dev/stdin').toString().trim().split(' ').map(num => parseInt(num)); |
// For local test |
// const input = [2, 1, 5]; // 4 |
// const input = [10, 3, 54]; //8 |
// const A = input.shift(); |
// const B = input.shift(); |
// const V = input.shift(); |
// console.log(Math.ceil((V - A) / (A - B)) + 1); |