🚀 들어가며...
- C++ STL Vector를 사용하여 풀이한다. (vector의 특성 상, front와 back 접근이 자유로워 편리하다)
- 단순 구현 문제이다.
🔗 문제
https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
💌 소스코드
#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 |
댓글