티스토리 뷰

Algorithm/BOJ

[C/C++] 백준 10866번 - 덱

poopooreum 2023. 9. 10. 20:15
반응형

✏️ 문제 링크

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

 

10866번: 덱

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

www.acmicpc.net

 

✏️ 문제 설명

 

✏️ 문제 코드

#include<iostream>
#include<deque>
#include<string>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int num;
	cin >> num;
	deque<int>q;
	for (int x = 0; x < num; x++) {
		string input;
		cin >> input;
		if (input == "push_front") {
			int number;
			cin >> number;
			q.push_front(number);
		}
		if (input == "push_back") {
			int number;
			cin >> number;
			q.push_back(number);
		}
		else if (input == "pop_front") {
			if (q.empty() == 1)
				cout << -1 << endl;
			else {
				cout << q.front() << endl;
				q.pop_front();
			}
		}
		else if (input == "pop_back") {
			if (q.empty() == 1)
				cout << -1 << endl;
			else {
				cout << q.back() << endl;
				q.pop_back();
			}
		}
		else if (input == "size") {
			if (q.empty() == 1)
				cout << 0 << endl;
			else
				cout << q.size()<<endl;
		}
		else if (input == "empty") {
			cout << q.empty() << endl;
		}
		else if (input == "front") {
			if (q.empty() == 1)
				cout << -1 << endl;
			else 
				cout << q.front() << endl;
		}
		else if (input == "back") {
			if (q.empty() == 1)
				cout << -1 << endl;
			else 
				cout << q.back() << endl;
		}
	}
}

 

반응형

'Algorithm > BOJ' 카테고리의 다른 글

[C/C++] 백준 10869번 - 사칙연산  (0) 2023.09.10
[C/C++] 백준 10867번 - 중복 빼고 정렬하기  (0) 2023.09.10
[C/C++] 백준 10845번 - 큐  (0) 2023.09.10
[C/C++] 백준 10844번 - 쉬운 계단 수  (0) 2023.09.10
백준 10828번 C++  (0) 2023.09.10
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함