728x90
문제 18258번
https://www.acmicpc.net/problem/18258
이번에는 큐이다. 입력값에 따라 조건문을 이용하여 문제에 나와있는 것처럼 수행해주도록 만든다.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
queue <int> q;
int n, x;
cin >> n;
string command;
while (n--) {
cin >> command;
if (command == "push") {
cin >> x;
q.push(x);
}
else if (command == "pop") {
if (q.empty()) {
cout << -1 << '\n';
}
else {
cout << q.front() << '\n';
q.pop();
}
}
else if (command == "size") {
cout << q.size() << '\n';
}
else if (command == "empty") {
if (q.empty()) {
cout << 1 << '\n';
}
else {
cout << 0 << '\n';
}
}
else if (command == "front") {
if (q.empty()) {
cout << -1 << '\n';
}
else {
cout << q.front() << '\n';
}
}
else if (command == "back") {
if (q.empty()) {
cout << -1 << '\n';
}
else {
cout << q.back() << '\n';
}
}
}
return 0;
}
728x90
'백준 > C++' 카테고리의 다른 글
[C++] 백준 11866번 - 요세푸스 문제 0 (0) | 2024.06.21 |
---|---|
[C++] 백준 2164번 - 카드 2 (0) | 2024.06.19 |
[C++] 백준 12789번 - 도키도키 간식드리미 (0) | 2024.06.17 |
[C++] 백준 28278번 - 스택 2 (0) | 2024.06.16 |
[C++] 백준 11478번 - 서로 다른 부분 문자열의 개수 (0) | 2024.06.14 |