Baekjoon algorithm training

[백준] 10250 (ACM 호텔)

interfacer_han 2023. 11. 23. 14:19

#1 알고리즘

#1-1

호텔에 몇 번째 손님이 어느 층 어느 방에 입실하는지의 규칙을 살펴본다.

 

#1-2

규칙을 공식으로 표현하면, 손님이 H의 배수인 경우와 아닌 경우로 나누어진다. 이 알고리즘을 그대로 코드로 만들어도 이 문제를 풀수 있다.

 

#1-3

3

이와같이 if문을 없애 코드의 길이를 줄이는 방법도 있다. 바로, 1-based indexing으로 순서가 기술되어 있는 손님을 0-based indexing으로 생각하는 것이다.

 

#2 코드

#2-1 자바

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int testCaseNumber = sc.nextInt();
        sc.nextLine();
        
        String[] submits = new String[testCaseNumber];
        
        for(int i = 0; i < submits.length; i++) {
            String[] testCase = sc.nextLine().trim().split(" ");
            submits[i] = allocatedRoomNumber(Integer.parseInt(testCase[0]), Integer.parseInt(testCase[1]), Integer.parseInt(testCase[2]));
        }
            
        sc.close();
        
        for(String submit : submits) {
            System.out.println(submit);
        }
    }
    
    private static String allocatedRoomNumber(int totalFloor, int totalWidth, int guestNumber) {
        int allocatedFloor = (guestNumber - 1) % totalFloor + 1;
        int allocatedRoomOfFloor = (guestNumber - 1) / totalFloor + 1;
        
        return Integer.toString(allocatedFloor) + String.format("%02d", allocatedRoomOfFloor);
    }
}

'1207'호실을 '127'과 같이 표현하지 않게 주의한다. String 클래스의 format() 메소드로 처리했다.

 

#2-2 코틀린

fun main() {
    val testCaseNumber = readln()!!.trim().toInt()

    val submits : Array<String> = Array(testCaseNumber) { "" }

    for(i : Int in 0..<testCaseNumber) {
        val testCase = readln()!!.trim().split(" ")
        submits[i] = allocatedRoomNumber(testCase[0].toInt(), testCase[1].toInt(), testCase[2].toInt())
    }

    for(submit : String in submits) {
        println(submit)
    }
}

fun allocatedRoomNumber(totalFloor : Int, totalWidth : Int, guestNumber: Int) : String {
    val allocatedFloor = (guestNumber - 1) % totalFloor + 1
    val allocatedRoomOfFloor = (guestNumber - 1) / totalFloor + 1

    return allocatedFloor.toString() + String.format("%02d", allocatedRoomOfFloor)
}

'1207'호실을 '127'과 같이 표현하지 않게 주의한다. String 클래스의 format() 메소드로 처리했다.

'Baekjoon algorithm training' 카테고리의 다른 글

[백준] 10828 (스택)  (0) 2023.11.25
[백준] 10818 (최소, 최대)  (0) 2023.11.24
[백준] 2920 (음계)  (0) 2023.11.22
[백준] 2884 (알람 시계)  (0) 2023.11.21
[백준] 11650 (좌표 정렬하기)  (0) 2023.11.18