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

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

 

2164๋ฒˆ: ์นด๋“œ2

N์žฅ์˜ ์นด๋“œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์นด๋“œ๋Š” ์ฐจ๋ก€๋กœ 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด ์žˆ์œผ๋ฉฐ, 1๋ฒˆ ์นด๋“œ๊ฐ€ ์ œ์ผ ์œ„์—, N๋ฒˆ ์นด๋“œ๊ฐ€ ์ œ์ผ ์•„๋ž˜์ธ ์ƒํƒœ๋กœ ์ˆœ์„œ๋Œ€๋กœ ์นด๋“œ๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋™์ž‘์„ ์นด๋“œ๊ฐ€

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/2164
// ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ž‘์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ
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)

	var queue []int
	for i := 1; i <= n; i++ {
		queue = append(queue, i)
	}

	var discard bool = true
	for len(queue) > 1 {
		if discard {
			queue = queue[1:]
		} else {
			queue = append(queue[1:], queue[0])
		}
		discard = !discard
	}

	fmt.Fprintln(writer, queue[0])
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/2164
# ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ž‘์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ
import sys
from collections import deque

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    q = deque(maxlen=n)

    for i in range(n):
        q.append(i+1)
    
    discard = True
    while len(q) > 1:
        if discard:
            q.popleft()
        else:
            q.append(q.popleft())
        discard = not discard
    
    print(q.pop())
728x90
๋Œ“๊ธ€