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

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

 

11866๋ฒˆ: ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ 0

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ N ≤ 1,000)

www.acmicpc.net


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

๐ŸŽจ Go

// https://www.acmicpc.net/problem/11866
// ํ๋ฅผ ์ด์šฉํ•ด ์ œ๊ฑฐ ๊ณผ์ •์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, k int
	fmt.Fscanln(reader, &n, &k)

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

	var delIdx int
	for len(queue) > 0 {
		delIdx = (delIdx + k - 1) % len(queue)
		result = append(result, queue[delIdx])
		queue = append(queue[:delIdx], queue[delIdx+1:]...)
	}

	for i, v := range result {
		if i == 0 {
			fmt.Fprintf(writer, "<%d", v)
		} else {
			fmt.Fprintf(writer, ", %d", v)
		}
	}
	fmt.Fprintln(writer, ">")
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/11866
# ํ๋ฅผ ์ด์šฉํ•ด ์ œ๊ฑฐ ๊ณผ์ •์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ
import sys
from collections import deque

if __name__ == "__main__":
    n, k = list(map(int, sys.stdin.readline().split()))

    queue = deque(maxlen=n)
    for i in range(1, n+1):
        queue.append(i)
    
    result = []
    del_idx = 0
    while len(queue) > 0:
        del_idx = (del_idx+k-1)%len(queue)
        result.append(queue[del_idx])
        del queue[del_idx]
    
    for i, v in enumerate(result):
        if i == 0:
            print("<{}".format(v), end='')
        else:
            print(", {}".format(v), end='')
    print(">")
728x90
๋Œ“๊ธ€