ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

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
๋Œ“๊ธ€