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


[알고리즘풀이]

자료구조 queue를 이용해서 문제에서 원하는대로 명령이 들어오면 명령을 실행하면 된다.

#include<iostream>
#include<queue>
#include<string>

using namespace std;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
	int n;
	queue<int> q;
	cin >> n;
	while (n--) {
		string input;
		cin >> input;
		if (input == "push") {
			int input2;
			cin >> input2;
			q.push(input2);
		}
		else if (input == "front") {
			if (q.empty())
				cout << "-1\n";
			else
				cout << q.front() << '\n';
		}
		else if (input == "pop") {
			if (q.empty())
				cout << "-1\n";
			else {
				cout << q.front() << '\n';
				q.pop();
			}
		}
		else if (input == "size") {
			cout << q.size() << '\n';
		}
		else if (input == "empty") {
			if (q.empty())
				cout << "1\n";
			else
				cout << "0\n";
		}
		else if (input == "front"){
			if (q.empty())
				cout << "-1\n";
			else
				cout << q.front() << '\n';
		}
		else {
			if (q.empty())
				cout << "-1\n";
			else
				cout << q.back() << '\n';
		}
	}
}

 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 11866 - 조세퍼스 문제 0  (0) 2019.09.17
[BOJ] 2164 - 카드2  (0) 2019.09.17
[BOJ] 17436 - 소수의 배수  (0) 2019.09.08
[BOJ] 9663 - N Queen  (0) 2019.09.08
[BOJ] 15652 - N 과 M (4)  (0) 2019.09.08

+ Recent posts