문제풀이/백준
[C++] 백준 - 10845 : 큐
orubt
2020. 3. 20. 12:48
반응형
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
www.acmicpc.net

풀이 ) 큐
자료구조 큐를 구현하는 문제이다.
입력받은 문자열을 통해 문자열에 해당하는 함수를 구현했다.
push( )를 구현할 때 매개변수를 넘겨받을 수 있지만 매개 변수 없이 구현했다.
#include <cstdio>
#include <string.h>
using namespace std;
int fr = -1, re = -1;
int queue[100002];
void push(){
int n;
scanf("%d", &n);
queue[++re] = n;
}
void pop(){
if(fr == re)
printf("-1\n");
else
printf("%d\n", queue[++fr]);
}
void size(){
printf("%d\n", re-fr);
}
void empty(){
if(fr == re)
printf("1\n");
else
printf("0\n");
}
void front(){
if(fr == re)
printf("-1\n");
else
printf("%d\n", queue[fr+1]);
}
void back(){
if(fr == re)
printf("-1\n");
else
printf("%d\n", queue[re]);
}
int main(void)
{
int n;
char str[10] = {0};
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%s", str);
if(!strcmp(str, "push"))
push();
else if(!strcmp(str, "pop"))
pop();
else if(!strcmp(str, "size"))
size();
else if(!strcmp(str, "empty"))
empty();
else if(!strcmp(str, "front"))
front();
else
back();
}
return 0;
}
반응형