dev/algorithm
BOJ / 10845번 / 큐 [Go][Python3]
crscnt
2021. 2. 1. 21:00
👩🏻💻 문제
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
✍🏻 풀이
🎨 Go
// https://www.acmicpc.net/problem/10845
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
queue := []int{}
for i := 0; i < n; i++ {
var command string
var number int
fmt.Fscanln(reader, &command, &number)
switch command {
case "push":
queue = append(queue, number)
case "pop":
output := -1
if len(queue) > 0 {
output = queue[0]
queue = queue[1:]
}
fmt.Fprintln(writer, output)
case "size":
fmt.Fprintln(writer, len(queue))
case "empty":
if len(queue) == 0 {
fmt.Fprintln(writer, 1)
} else {
fmt.Fprintln(writer, 0)
}
case "front":
output := -1
if len(queue) > 0 {
output = queue[0]
}
fmt.Fprintln(writer, output)
case "back":
output := -1
if len(queue) > 0 {
output = queue[len(queue)-1]
}
fmt.Fprintln(writer, output)
}
}
}
🎨 Python3
# https://www.acmicpc.net/problem/10845
import sys
if __name__ == "__main__":
n = int(sys.stdin.readline())
queue = []
for i in range(n):
inputs = sys.stdin.readline().split()
command = inputs[0]
number = 0
if len(inputs) == 2:
number = inputs[1]
if command == "push":
queue.append(number)
elif command == "pop":
output = -1
if len(queue) > 0:
output = queue[0]
queue.pop(0)
print(output)
elif command == "size":
print(len(queue))
elif command == "empty":
if len(queue) == 0:
print(1)
else:
print(0)
elif command == "front":
output = -1
if len(queue) > 0:
output = queue[0]
print(output)
elif command == "back":
output = -1
if len(queue) > 0:
output = queue[len(queue)-1]
print(output)
728x90