Study hard

(c++)백준 10845번: 큐 본문

백준/여러가지 문제들

(c++)백준 10845번: 큐

Nimgnoej 2020. 6. 12. 20:43

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

 

10845번: 큐

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

www.acmicpc.net

 

[풀이]

C++ STL의 queue를 사용하여 풀 수 있다.

각 명령어를 입력받는 동시에 queue 명령을 처리하면 된다.

 

#include <iostream>
#include <queue>
#include <string>
using namespace std;

int N;
string str;
queue<int>q;

void solution() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> str;
		if (str == "push") {
			int pushNum;
			cin >> pushNum;
			q.push(pushNum);
		}
		else if (str == "pop") {
			if (q.empty()) {
				cout << -1 << endl;
				continue;
			}
			cout << q.front() << endl;
			q.pop();
		}
		else if (str == "size") {
			cout << q.size() << endl;
		}
		else if (str == "empty") {
			cout << q.empty() << endl;
		}
		else if (str == "front") {
			if (q.empty()) {
				cout << -1 << endl;
				continue;
			}
			cout << q.front() << endl;
		}
		else if (str == "back") {
			if (q.empty()) {
				cout << -1 << endl;
				continue;
			}
			cout << q.back() << endl;
		}
	}
}

int main() {
	solution();
	return 0;
}

'백준 > 여러가지 문제들' 카테고리의 다른 글

(c++)백준 10808번: 알파벳 개수  (0) 2020.06.12
(c++)백준 10866번: 덱  (0) 2020.06.12
(c++)백준 10799번: 쇠막대기  (0) 2020.06.12
(c++)백준 9012번: 괄호  (0) 2020.06.12
(c++)백준 10828번: 스택  (0) 2020.06.11