알고리즘/boj
BOJ 10845 큐
칼퇴시켜주세요
2023. 2. 10. 20:37
728x90
이 문제는 STL의 Queue 사용법에 대해 알고있으면 쉽게 해결할 수 있는 문제이다.
풀이 코드는 다음과 같다.
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
queue<int> q;
for(int i = 0; i < n; i++)
{
string method;
cin >> method;
if(method == "push")
{
int num;
cin >> num;
q.push(num);
}
else if(method == "size")
{
cout << q.size() << "\n";
}
else if(method == "empty")
{
if(q.empty())
{
cout << 1 << "\n";
}
else
{
cout << 0 << "\n";
}
}
else
{
if(q.empty())
{
cout << -1 << "\n";
}
else
{
if(method == "pop")
{
cout << q.front() << "\n";
q.pop();
}
else if(method == "front")
{
cout << q.front() << "\n";
}
else if(method == "back")
{
cout << q.back()<<"\n";
}
}
}
}
}
반응형