항해 99클럽 코테
99클럽 코테 스터디 14일차 TIL
metamong-data
2024. 11. 11. 09:32
728x90
반응형
오늘의 학습 키워드
해시
과제
문제
정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
명령은 총 여섯 가지이다.
- push X: 정수 X를 큐에 넣는 연산이다.
- pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- size: 큐에 들어있는 정수의 개수를 출력한다.
- empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
- front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
입력
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
출력
출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.
예제 입력 1 복사
15
push 1
push 2
front
back
size
empty
pop
pop
pop
size
empty
pop
push 3
empty
front
예제 출력 1 복사
1
2
2
0
1
2
-1
0
1
-1
0
3
풀이 과정
- 명령어 개수 입력 받고
- 큐 역할을 할 리스트를 만들어서
- if 문을 이용해서 큐 역할을 하도록 만든다.제출 코드 (python)
import sys
N = int(sys.stdin.readline().strip())
stack = []
for i in range(N):
cmd = sys.stdin.readline().strip().split()
if cmd[0] == 'push':
stack.append(cmd[1])
elif cmd[0] == 'pop':
if len(stack) != 0:
print(stack.pop(0))
else:
print(-1)
elif cmd[0] == 'size':
print(len(stack))
elif cmd[0] == 'empty':
if len(stack) == 0:
print(1)
else:
print(0)
elif cmd[0] == 'front':
if len(stack) != 0:
print(stack[0])
else:
print(-1)
elif cmd[0] == 'back':
if len(stack) != 0:
print(stack[-1])
else:
print(-1)
다른 언어로 문제 풀기
GO 언어
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var N int
fmt.Fscanln(reader, &N)
queue := make([]string, 0)
for i := 0; i < N; i++ {
line, _ := reader.ReadString('\n')
line = strings.TrimSpace(line)
cmd := strings.Split(line, " ")
switch cmd[0] {
case "push":
queue = append(queue, cmd[1])
case "pop":
if len(queue) != 0 {
fmt.Fprintln(writer, queue[0])
queue = queue[1:]
} else {
fmt.Fprintln(writer, -1)
}
case "size":
fmt.Fprintln(writer, len(queue))
case "empty":
if len(queue) == 0 {
fmt.Fprintln(writer, 1)
} else {
fmt.Fprintln(writer, 0)
}
case "front":
if len(queue) != 0 {
fmt.Fprintln(writer, queue[0])
} else {
fmt.Fprintln(writer, -1)
}
case "back":
if len(queue) != 0 {
fmt.Fprintln(writer, queue[len(queue)-1])
} else {
fmt.Fprintln(writer, -1)
}
}
}
}
C++
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
deque<string> queue;
string cmd;
for(int i = 0; i < N; i++) {
cin >> cmd;
if(cmd == "push") {
string x;
cin >> x;
queue.push_back(x);
}
else if(cmd == "pop") {
if(!queue.empty()) {
cout << queue.front() << '\n';
queue.pop_front();
} else {
cout << "-1\n";
}
}
else if(cmd == "size") {
cout << queue.size() << '\n';
}
else if(cmd == "empty") {
cout << (queue.empty() ? 1 : 0) << '\n';
}
else if(cmd == "front") {
if(!queue.empty()) {
cout << queue.front() << '\n';
} else {
cout << "-1\n";
}
}
else if(cmd == "back") {
if(!queue.empty()) {
cout << queue.back() << '\n';
} else {
cout << "-1\n";
}
}
}
return 0;
}
오늘의 회고
- queue를 IF문으로만 구현해서 만들어서 queue에 대해서 이해를 더 잘 할 수 있게 되는 것 같다
728x90