Study hard
(c++)백준 10845번: 큐 본문
https://www.acmicpc.net/problem/10845
[풀이]
C++ STL의 queue를 사용하여 풀 수 있다.
각 명령어를 입력받는 동시에 queue 명령을 처리하면 된다.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int N;
string str;
queue<int>q;
void solution() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> str;
if (str == "push") {
int pushNum;
cin >> pushNum;
q.push(pushNum);
}
else if (str == "pop") {
if (q.empty()) {
cout << -1 << endl;
continue;
}
cout << q.front() << endl;
q.pop();
}
else if (str == "size") {
cout << q.size() << endl;
}
else if (str == "empty") {
cout << q.empty() << endl;
}
else if (str == "front") {
if (q.empty()) {
cout << -1 << endl;
continue;
}
cout << q.front() << endl;
}
else if (str == "back") {
if (q.empty()) {
cout << -1 << endl;
continue;
}
cout << q.back() << endl;
}
}
}
int main() {
solution();
return 0;
}
'백준 > 여러가지 문제들' 카테고리의 다른 글
(c++)백준 10808번: 알파벳 개수 (0) | 2020.06.12 |
---|---|
(c++)백준 10866번: 덱 (0) | 2020.06.12 |
(c++)백준 10799번: 쇠막대기 (0) | 2020.06.12 |
(c++)백준 9012번: 괄호 (0) | 2020.06.12 |
(c++)백준 10828번: 스택 (0) | 2020.06.11 |