ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 3986๋ฒ / ์ข์ ๋จ์ด [Go][Python3] (0) | 2021.02.03 |
---|---|
BOJ / 1620๋ฒ / ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ [Go][Python3] (0) | 2021.02.02 |
BOJ / 1158๋ฒ / ์์ธํธ์ค ๋ฌธ์ [Go][Python3] (0) | 2021.01.31 |
BOJ / 5568๋ฒ / ์นด๋ ๋๊ธฐ [Go][Python3] (0) | 2021.01.30 |
BOJ / 2303๋ฒ / ์ซ์ ๊ฒ์ [Go][Python3] (0) | 2021.01.29 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์๊ณ ๋ฆฌ์ฆ
- dp
- ๋ฐฑ์ค
- AWS
- ๋งฅ๋ถํ๋ก
- ๋ชฝ๊ณ ๋๋น
- ์๋ฐ
- ์ด๋ถํ์
- python3
- Macbook pro 2012 mid 13
- ํ
- Algorithm
- ์๊ฐ๊ต์ฒด
- ballet
- go
- ๋ถํ ์ ๋ณต
- MongoDB
- ๋ธ๋ฃจํธํฌ์ค
- ๋ฐ๋
- ํ๋ก์ด๋์์ฌ
- dfs
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋งฅ๋ถ
- ์คํ
- ํด์๋งต
- BOJ
- BFS
- Golang
- java
- baekjoon
- Total
- Today
- Yesterday