알고리즘/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";
				}
			}
		}
	}
}

 

반응형

'알고리즘 > boj' 카테고리의 다른 글

BOJ 18111 마인크래프트  (0) 2023.02.13
BOJ 10816 숫자 카드 2  (0) 2023.02.10
BOJ 11050 이항 계수 1  (0) 2023.02.08
BOJ 7662 이중 우선순위 큐  (0) 2023.02.05
BOJ 18870 좌표 압축  (0) 2023.02.05