10845
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// push 함수
void push_f (int *queue, int index){
int value = 0;
scanf("%d", &value);
queue[index] = value;
}
int main(void){
const char* push = "push";
const char* pop = "pop";
const char* size = "size";
const char* empty = "empty";
const char* front = "front";
const char* back = "back";
// 테스트 케이스 입력
int test_case = 0;
scanf("%d", &test_case);
int *queue = malloc(sizeof(int)*test_case);
int start = 0, end = 0;
for(int i = 0; i < test_case; i++){
// 명령어 입력
char command [6] = {0};
scanf("%s", command);
if(strcmp(command, push) == 0){// push
push_f(queue, end++);
}
else if(strcmp(command, pop) == 0){// pop
if(start == end){
printf("-1\n");
}
else{
printf("%d\n", queue[start++]);
}
}
else if(strcmp(command, size) == 0){// size
printf("%d\n", (end-start));
}
else if(strcmp(command, empty) == 0){// empty
printf("%d\n", (start == end));
}
else if(strcmp(command, front) == 0){// front
if(start == end){
printf("-1\n");
}
else{
printf("%d\n", queue[start]);
}
}
else if(strcmp(command, back) == 0){// back
if(start == end){
printf("-1\n");
}
else{
printf("%d\n", queue[end-1]);
}
}
}
free(queue);
return 0;
}

시간 제한이 있어서 queue를 완벽하게 구현하기보단 주어진 조건을 만족하면서 빠르게 끝내는 방법을 선택했다
'백준' 카테고리의 다른 글
| [ 백준 / C ] 10828번 : 스택 (0) | 2025.11.21 |
|---|---|
| [ 백준 / C ] 5635번 : 생일 (0) | 2025.11.20 |
| [ 백준 / C ] 12778번 : CTP공국으로 이민 가자 (0) | 2025.11.17 |
| [ 백준 / C ] 13877번 : 이건 무슨 진법이지? (0) | 2025.11.14 |
| [ 백준 / C ] 9455번 : 박스 (0) | 2025.11.13 |