본문 바로가기

알고리즘

백준온라인 - 수학1 - ACM 호텔

문제 정보

문제

풀이

  • 출구로부터 가까운 거리 기준으로는 저층부터 고층까지 1번방부터 예약하고.
    각 층의 1번방이 다 차면 2번방, 3번방 차례로 체워나감
  • 모든 층의 N 번째 호수가 체워져야 다음 N+1 번 호수가 체워진다.
  • (차례 / 층수) = N
  • 나눠진 몫 만큼 호수가 다 체워진 것을 의미
    나머지가 있다면 전체 층의 해당 호수가 체워진 것이므로 몫 +1 = 호수
    나머지가 없다면 몫 = 호수
  • 나머지가 없다면, 다음 호수까지 갈 필요가 없으므로, 최상층을 의미 = 층수
    나머지가 있다면, 나머지 값이 현재 채워야할 호수의 층

코드

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

    private static final DecimalFormat decimalFormat = new DecimalFormat("00");

    public static String solve(int height, int width, int number) {

        int divided = number / height;
        int rest = number % height; // 층

        int targetHeight = rest == 0 ? height : rest;
        int targetWidth = divided + (rest == 0 ? 0 : 1);

        return targetHeight + decimalFormat.format(targetWidth);

    }

    public static void main(String[] args) {
        // 6 12 10
        // 30 50 72
        Scanner scanner = new Scanner(System.in);
        int count = Integer.parseInt(scanner.nextLine());

        for (int i = 0 ; i < count; i++) {
            String[] test = scanner.nextLine().split("\\s");
            System.out.println(solve(Integer.parseInt(test[0]), Integer.parseInt(test[1]), Integer.parseInt(test[2])));
        }

//        int height = 30;
//        int width = 50;
//        int number = 72;
    }
}