ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/20301
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, k, m int
fmt.Fscanln(reader, &n, &k, &m)
queue := []int{}
for i := 0; i < n; i++ {
queue = append(queue, i+1)
}
selected := 0
removed := []int{}
turnRight := false
for len(queue) > 0 {
if len(removed)%m == 0 {
turnRight = !turnRight
}
if turnRight {
selected = (selected + k - 1) % len(queue)
} else {
selected = (selected - k) % len(queue)
for selected < 0 {
selected += len(queue)
}
}
removed = append(removed, queue[selected])
queue = append(queue[:selected], queue[selected+1:]...)
}
for i := 0; i < n; i++ {
fmt.Fprintln(writer, removed[i])
}
}
๐จ Python3
# https://www.acmicpc.net/problem/20301
import sys
if __name__ == "__main__":
n, k, m = list(map(int, sys.stdin.readline().split()))
queue = [i+1 for i in range(n)]
selected = 0
removed = []
turn_right = False
while len(queue) > 0:
if len(removed) % m == 0:
turn_right = not turn_right
if turn_right:
selected = (selected + k - 1) % len(queue)
else:
selected = (selected - k) % len(queue)
while selected < 0:
selected += len(queue)
removed.append(queue[selected])
queue.pop(selected)
for i in range(n):
print(removed[i])
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1789๋ฒ / ์๋ค์ ํฉ [Go][Python3] (0) | 2021.02.26 |
---|---|
BOJ / 10994๋ฒ / ๋ณ ์ฐ๊ธฐ 19 [Go][Python3] (0) | 2021.02.25 |
BOJ / 12789๋ฒ / ๋ํค๋ํค ๊ฐ์๋๋ฆฌ๋ฏธ [Go][Python3] (0) | 2021.02.23 |
BOJ / 6187๋ฒ / Going to the Movies [Go][Python3] (0) | 2021.02.22 |
BOJ / 3048๋ฒ / ๊ฐ๋ฏธ [Go][Python3] (0) | 2021.02.21 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ํ๋ก์ด๋์์ฌ
- Macbook pro 2012 mid 13
- ํ
- ์๊ฐ๊ต์ฒด
- ๋งฅ๋ถ
- ๋ฐฑ์ค
- Algorithm
- java
- BOJ
- ์ด๋ถํ์
- go
- ๋ฐ๋
- ๋ถํ ์ ๋ณต
- MongoDB
- ๋งฅ๋ถํ๋ก
- Golang
- AWS
- BFS
- ์๊ณ ๋ฆฌ์ฆ
- baekjoon
- ballet
- ๋ชฝ๊ณ ๋๋น
- ๋ธ๋ฃจํธํฌ์ค
- dp
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์คํ
- ํด์๋งต
- ์๋ฐ
- dfs
- python3
- Total
- Today
- Yesterday