백준-Node.js-10250, ACM 호텔

송철진·2023년 3월 6일
0

백준-Node.js

목록 보기
47/69

문제

https://www.acmicpc.net/problem/10250

풀이

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim()
                .split('\n').slice(1).map(el => el.split(' '))

const solution = input =>{
  return input.map(el => {
    let [h, w, n] = el
    if(n % h === 0 ){
      return h + ( '0' + (parseInt(n/h)) ).slice(-2)
    }else{
      return n%h + ( '0' + (1 + parseInt(n/h)) ).slice(-2)
    } 
  }).join('\n')
}
console.log(solution(input))

slice(-2)를 substring(-2)로 구현했을 때는 틀렸다고 표시된다.
왜일까?

substring()의 start값이 음수이면 start값을 0으로 취급하기 때문이다. - 출처

'012'.substring(-2)	// 012
'012'.slice(-2)		// 12
profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글