택배 상자 꺼내기

이리·2025년 3월 1일

문제: https://school.programmers.co.kr/learn/courses/30/lessons/389478

문제설명

  • 파라미터: int n, int w, int num
  • 반환값: int
  • 교차로 쌓아가는 택배에서 자신의 번호를 꺼낼때 자신 포함 몇개의 상자를 꺼내야하는지 return

풀이방식

  1. 본인이 몇번째 줄, 몇번째 위치인지 알아야한다. + 마지막 상자가 몇번째 줄, 몇번째 위치인지 알아야한다.

  2. 만약 상자가 w로 나누었을때 나머지가 0이라면 위치를 조정시킬 필요가 있다.

    → col == 0 → row— , col = w

  3. 자신의 위로 몇줄이 있는지 알아야한다.

  4. 마지막 줄에 자신의 col이 존재하는지 알아야한다.

코드

class Solution {
    public int solution(int n, int w, int num) {
        int answer = 0;
        int row = num / w + 1 ;
        int col = num % w;
        if(col == 0){
            row--;
            col = w;
        }
        int limitRow = n / w + 1;
        int limitCol = n % w;
        if(limitCol == 0){
            limitRow--;
            limitCol = w;
        }
        boolean even = true; 
        // 진행방향 확인 
        int curDir = 0; // 0 우 1 좌 
        int numDir = 0;
        
        if(row % 2 == 0) curDir = 1;
        if(limitRow % 2 == 0) numDir = 1;
        
        System.out.println(col + " " + limitCol);
        
        if(curDir == numDir){
            answer = limitRow - row + 1;
            System.out.println(col + " " + limitCol);
            if(col > limitCol) answer--;
            
        }else{
            answer = limitRow - row + 1;
            col = w - col + 1;
            if(col > limitCol) answer--;
        }
        
        return answer;
    }
}
```![](https://velog.velcdn.com/images/illli_705/post/60aa00b4-cdc4-4566-b463-cc6de2a83812/image.png)

0개의 댓글