C++
(C++) [백준] 큐
JJeongHyun
2023. 1. 10. 14:33
반응형
https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
요약)
- 첫번째 입력 받는 수는 처리할 명령의 수를 입력한다
- 두번째 입력은 문제에 나온 큐에 대한 명령어를 하나 씩 입력한다
- 정수는 1 이상 100,000 이하의 수이다
- 문제내에 포함되어 있지 않은 명령이 나올 경우는 없다
- 출력하는 명령에 대한 결과는 한 줄에 하나 씩 출력한다
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
queue<int> qu;
for (int i = 0; i < n; i++) {
string input;
cin >> input;
if (input == "push") {
int x;
cin >> x;
qu.push(x);
}
else if (input == "pop") {
if (qu.empty()) cout << "-1" << endl;
else {
cout << qu.front() << endl;
qu.pop();
}
}
else if (input == "size")
cout << qu.size() << endl;
else if (input == "empty") {
if (qu.size() == 0) cout << "1" << endl;
else cout << "0" << endl;
}
else if (input == "front") {
if (qu.empty()) cout << "-1" << endl;
else cout << qu.front() << endl;
}
else if(input == "back") {
if (qu.empty()) cout << "-1" << endl;
else cout << qu.back() << endl;
}
}
return 0;
}