10845 큐

 

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

 

10845번: 큐

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

www.acmicpc.net

 

아니 이거 왜 안풀어져있는지 의문이었다.

그래서 풀었다.

단순 큐 구현 문제이다.

그래서 정석으로 구현해서 풀어보았다.

 

#include <stdio.h>
int front=0,rear=0,queue[10001];

void push(int x) {
    queue[rear]=x;
    rear++;
}

void pop() {
    if(front<rear) {
        printf("%d\n", queue[front]);
        front++;
    }
    else puts("-1");
}

int main()
{
    int t,n,i;
    char str[9];
    scanf("%d", &t);
    for(i=0; i<t; i++) {
        scanf("%s", str);
        if(str[1]=='u') {
            scanf("%d", &n);
            push(n);
        }
        else if(str[0]=='p') pop();
        else if(str[0]=='s') printf("%d\n",rear-front);
        else if(str[0]=='e') {
            if(front==rear) puts("1");
            else puts("0");
        }
        else if(str[0]=='f') {
            if(front<rear) printf("%d\n",queue[front]);
            else puts("-1");
        }
        else if(str[0]=='b') {
            if(front<rear) printf("%d\n",queue[rear-1]);
            else puts("-1");
        }
    }
}

+ Recent posts