문제 링크

 

CPP

#include <bits/stdc++.h>
using namespace std;

int n;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    queue<int> q;
    while(n--){  //n번 반복
      string c;  
      cin >> c;  // 명령어 입력 받기 
      if(c=="push"){
      int t;
      cin >> t;
      q.push(t);
    }
    else if(c=="pop"){
      if(q.empty()) cout << -1 << '\n';
      else{
        cout << q.front() << '\n';
        q.pop();
      }
    }
    else if(c=="size")
      cout << q.size() << '\n';
    else if(c=="empty")
      cout << (int)q.empty() << '\n';
    else if(c=="front"){
      if(q.empty()) cout << -1 << '\n';
      else cout << q.front() << '\n';
    }
    else{ //back
      if(q.empty()) cout << -1 << '\n';
      else cout << q.back() << '\n';
    }
}
}

'CPP 문제풀이 > 백준' 카테고리의 다른 글

백준 1181번 : 단어 정렬  (0) 2020.09.22
백준 1026번 : 보물  (0) 2020.09.22
백준 10815번 : 숫자 카드  (0) 2020.09.22
백준 10989번 : 수 정렬하기 3  (0) 2020.09.18
백준 1920번 : 수 찾기  (0) 2020.09.17
블로그 이미지

hjc_

୧( “̮ )୨

,