본문 바로가기
코딩테스트_백준풀이

큐 #10845 c++ 풀이

by wanna_dev 2023. 10. 13.

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

 

10845번: 큐

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

www.acmicpc.net

저번에 풀었던 스택에서 4-5줄 정도 바꾸니까 풀렸습니다.

역시 헤더파일에서 queue를 포함시켜주었습니다.

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
#include<string>
#include<stack>
using namespace std;



int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int N;
	queue<int> s;
	string order;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> order;
		if (order == "push") {
			int num;
			cin >> num;
			s.push(num);
		}
		else if (order == "pop") {
			if (s.empty()) {
				cout << "-1" << '\n';
				continue;
			}
			cout << s.front() << '\n';
			s.pop();
		}
		else if (order == "size") {
			cout << s.size() << '\n';
		}
		else if (order == "empty") {
			if (s.empty()) {
				cout << "1" << '\n';
			}
			else cout << "0" << '\n';

		}
		else if(order == "front") {
			if (s.empty())cout << "-1" << '\n';
			else {
				cout << s.front() << '\n';
			}
		}
		else {
			if (s.empty())cout << "-1" << '\n';
			else {
				cout << s.back() << '\n';
			}
		}
		
		

	}
	
	return 0;
}

'코딩테스트_백준풀이' 카테고리의 다른 글

숫자 카드 2 #10816 c++ 풀이  (1) 2023.10.13
덱 #10866 c++ 풀이  (1) 2023.10.13
스택 #10828 c++ 풀이  (0) 2023.10.13
요세푸스 문제 0 #11866 c++ 풀이  (0) 2023.10.12
나이순 정렬 #10814 c++ 풀이  (0) 2023.10.12