백준 15828번: Router

Se0ng_1l·2022년 7월 16일
0

백준

목록 보기
39/40

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

📌문제접근

  1. 큐와 관련 함수를 활용한다.
  2. 양수일때 큐의 크기와 버퍼의 크기를 비교한다
  3. 만약 큐의 크기가 작다면 push하고 그렇지 않다면 버린다.
  4. 0일때는 pop, -1일 때는 반복문을 종료시킨다.
  5. 큐가 비어있는지 확인하고 비어있지 않다면 pop을 해가면서 출력
#include <iostream>
#include <queue>
using namespace std;

int main(){
    int size;
    cin >> size;
    int num;
    queue<int> q;

    while(true)
    {
        cin >> num;
        if(num == -1)
            break;
        if(num > 0)
        {
            if(q.size() < size)
                q.push(num);
        }
        if(num == 0)
            q.pop();
    }
    if(q.empty())
        cout << "empty" << endl;
    else
        while(!q.empty())
        {
            cout << q.front() << ' ';
            q.pop();
        }
}
profile
치타가 되고 싶은 취준생

0개의 댓글