[백준] 10250. ACM 호텔

상현·2023년 10월 25일
0

코딩테스트

목록 보기
6/30
post-thumbnail

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

처음 풀이

let input = "2\n6 12 10\n30 50 72".toString().trim().split('\n');
const testLength = input[0];
for (let i = 1; i <= testLength; i++) {
  const [height, width, n] = input[i].split(" ");
  let count = 0;
  for (let room = 1; room <= width; room++) {
    for (let floor = 1; floor <= height; floor++) {
      count++;
      if (count === +n) {
        console.log(`${floor}${(room + "").padStart(2,0)}`);
        break;
      }
    }
  }
}

처음에는 층과 방을 돌면서 그냥 for문을 돌면 되겠지 하고 단순하게 생각했다.. 근데 문제를 풀수록 뭔가 더 간단한 사칙연산으로 풀 수 있을 것 같았다.

최종 제출

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');

const testLength = input[0];
for (let i = 1; i <= testLength; i++) {
  const [height, , n] = input[i].split(" ");
  const floor = n % height === 0 ? height : n % height;
  console.log(`${floor}${(Math.ceil(n / height) + "").padStart(2, "0")}`)
}

역시 나머지와 나눗셈을 잘 이용해서 계산하면 쉽게 풀 수 있었다.

profile
프론트엔드 개발자 🧑🏻‍💻 https://until.blog/@love

0개의 댓글