본문 바로가기
백준 문제 풀이 & C++ 공부

백준 10845번 C++

by daisy0461 2021. 8. 29.

https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

아마 다들 이 이전에 올렸던 스택 문제를 푸셨으면 C++로는 쉽게 풀었을 것같습니다.

동일하게 queue 헤더 파일을 사용할 수 있는지를 묻는 문제였습니다.

따로 활용할 필요는 없었고 그대로 문자열을 받아 동일하게 수행하면 됩니다.

 

#include <iostream>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

int main(int argc, const char* argv[]) {
	int n;			//명령 수
	queue<int> orderQueue;

	cin >> n;

	for (int i = 0; i < n; i++) {
		string a;
		cin >> a;
		if (a == "push") {
			int number;
			cin.ignore();
			cin >> number;
			cin.ignore();

			orderQueue.push(number);
		}
		else if(a == "front")
		{
			if (orderQueue.empty()) {		//stack이 비어있다면 -1 출력
				cout << "-1" << endl;
			}
			else
			{
				cout << orderQueue.front() << endl;
			}
		}
		else if (a == "back")
		{
			if (orderQueue.empty()) {		//stack이 비어있다면 -1 출력
				cout << "-1" << endl;
			}
			else
			{
				cout << orderQueue.back() << endl;
			}
		}
		else if (a == "size") {
			cout << orderQueue.size() << endl;
		}
		else if (a == "empty") {
			if (orderQueue.empty()) {
				cout << "1" << endl;
			}
			else {
				cout << "0" << endl;
			}
		}
		else if (a == "pop") {
			if (orderQueue.empty()) {
				cout << "-1" << endl;
			}
			else {
				int popNumber;
				popNumber = orderQueue.front();
				cout << popNumber << endl;
				orderQueue.pop();
			}
		}
	}
}

'백준 문제 풀이 & C++ 공부' 카테고리의 다른 글

백준 11866번 C++  (0) 2021.09.01
백준 18111번 C++  (0) 2021.09.01
백준 10828번 C++  (0) 2021.08.19
백준 10816번 C++ & upper_bound, lower_bound  (0) 2021.08.18
백준 10814번 C++  (0) 2021.07.07