-
[백준 10845] 큐 (cpp, vector)알고리즘/백준 2021. 12. 19. 03:55
🚀 들어가며...
- C++ STL Vector를 사용하여 풀이한다. (vector의 특성 상, front와 back 접근이 자유로워 편리하다)
- 단순 구현 문제이다.
🔗 문제
https://www.acmicpc.net/problem/10845
💌 소스코드
#include <iostream> #include <vector> #include <string> using namespace std; int main(void){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<int> v; int cnt; cin >> cnt; while(cnt--){ string cmd; cin >> cmd; if(cmd == "push"){ int n; cin >> n; v.push_back(n); } else if(cmd == "pop"){ // 맨 앞의 원소를 출력하고 제거한다 if(!v.empty()){ cout << v.front() << endl; v.erase(v.begin()); } else cout << -1 << endl; } else if(cmd == "size"){ cout << v.size() << endl; } else if(cmd == "empty"){ if(!v.empty()) cout << 0 << endl; else cout << 1 << endl; } else if(cmd == "front"){ if(v.empty()) cout << -1 << endl; else cout << v.front() << endl; } else if(cmd == "back"){ if(v.empty()) cout << -1 << endl; else cout << v.back() << endl; } } }
반응형'알고리즘 > 백준' 카테고리의 다른 글
[백준 17413] 단어 뒤집기 2 (0) 2021.12.19 [백준 1158] 요세푸스 문제 (cpp, queue) (0) 2021.12.19 [백준 1406] 에디터 (cpp, stack) (0) 2021.12.19 [백준 1874] 스택 수열 (cpp, stack) (0) 2021.12.19