본문 바로가기

알고리즘 문제

[C/C++ 백준 10845번] 큐 (Silver 4)

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

 

10845번: 큐

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

www.acmicpc.net

STL에서 제공하는 queue자료형을 이용해서, 주어진 명령을 수행하자.

 

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int main(void){
	int N, x;
	scanf("%d", &N);
	queue<int> q;
	char str[100];
	char str1[100]="push";
	char str2[100]="front";
	char str3[100]="back";
	char str4[100]="size";
	char str5[100]="pop";
	char str6[100]="empty";
	for(int i=0; i<N; i++){
		scanf(" %s", str);
		if(strcmp(str, str1)==0){
			scanf("%d",&x);
			q.push(x);
		}
		if(strcmp(str, str2)==0){
			if(q.empty())
				printf("-1\n");
			else
				printf("%d\n",q.front());
		}
		if(strcmp(str, str3)==0){
			if(q.empty())
				printf("-1\n");
			else
				printf("%d\n",q.back());
		}
		if(strcmp(str, str4)==0){
			printf("%d\n",q.size());
		}
		if(strcmp(str, str5)==0){
			if(q.empty())
				printf("-1\n");
			else{
				printf("%d\n",q.front());
				q.pop();
			}
		}
		if(strcmp(str, str6)==0){
			printf("%d\n",q.empty());
		}
	}
}