본문 바로가기
  • Let's study
PS/Programmers

[프로그래머스 Lv.1] 택배 상자 꺼내기(C++)

by 코딩고수이고파 2025. 3. 20.

문제

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

코드

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int solution(int n, int w, int num) {
    int answer = 0;
    vector<vector<int>>v;
    int h=(n-1)/w+1;
    int x=1;
    
    for(int k=0;k<h;k++){
        vector<int>list;
        for(int i=1;i<=w;i++){
            if(x <= n)
                list.push_back(x++);
        }
        
        if(x>n){
            for(int i=0;i<(w - (n % w)) % w;i++){   //마지막에 %w를 추가해야 꽉 찬 경우도 고려려
                list.push_back(0);
            }
        }
        if(k%2!=0)
            reverse(list.begin(), list.end());
    
        v.push_back(list);
    }

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){  
            if(v[i][j]==num){
                 answer=h-i;
                
                if(v[h-1][j]==0)
                    answer--;
                    
                return answer;
            }   
        }
    }

}

댓글