개발 공부~

[백준 - 10250] ACM 호텔 .java 본문

코딩테스트/백준

[백준 - 10250] ACM 호텔 .java

머밍 2025. 5. 1. 23:13

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

 

 

내 풀이

계산식으로 풀었다 다만 꼭대기 층에 배정될 때 층이 0이 되며 방번호도 1 증가하는 부분만 조건식으로 풀었다

import java.util.Scanner;

public class boj {

    public static void main(String[] args)  {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int h = sc.nextInt();
            int w = sc.nextInt();
            int n = sc.nextInt();

            int f = n%h;
            int num = n/h+1;
            if(f==0) {
                f = h;
                num--;}

            System.out.printf("%d%02d\n",f,num);
        }


    }
}

 

 

개선된 풀이

int f = (n - 1) % h + 1;
int num = (n - 1) / h + 1;

 

n-1을 사용해서 0부터 시작하는 인덱스처럼 취급했다 

-> 조건문 없이도 간결해짐!