ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1158
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)
result := Josephus(n, k)
for i := 0; i < len(result); i++ {
if i == 0 {
fmt.Fprintf(writer, "<%d", result[i])
} else {
fmt.Fprintf(writer, ", %d", result[i])
}
}
fmt.Fprintln(writer, ">")
}
func Josephus(n, k int) (result []int) {
queue := []int{}
for i := 1; i <= n; i++ {
queue = append(queue, i)
}
index := 0
for len(queue) > 0 {
index = (index + k - 1) % len(queue)
result = append(result, queue[index])
queue = append(queue[:index], queue[index+1:]...)
}
return result
}
๐จ Python3
# https://www.acmicpc.net/problem/1158
import sys
def josephus(n, k):
queue = [i for i in range(1, n+1)]
index = 0
result = []
while len(queue) > 0:
index = (index + k-1) % len(queue)
result.append(queue[index])
queue.pop(index)
return result
if __name__ == "__main__":
n, k = list(map(int, sys.stdin.readline().split()))
result = josephus(n, k)
for i in range(len(result)):
if i == 0:
print("<{}".format(result[i]), end='')
else:
print(", {}".format(result[i]), end='')
print(">")
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1620๋ฒ / ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ [Go][Python3] (0) | 2021.02.02 |
---|---|
BOJ / 10845๋ฒ / ํ [Go][Python3] (0) | 2021.02.01 |
BOJ / 5568๋ฒ / ์นด๋ ๋๊ธฐ [Go][Python3] (0) | 2021.01.30 |
BOJ / 2303๋ฒ / ์ซ์ ๊ฒ์ [Go][Python3] (0) | 2021.01.29 |
BOJ / 2635๋ฒ / ์ ์ด์ด๊ฐ๊ธฐ [Go][Python3] (0) | 2021.01.28 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- python3
- BFS
- ๋ฐ๋
- ๋ธ๋ฃจํธํฌ์ค
- ์ด๋ถํ์
- MongoDB
- AWS
- ํ
- ballet
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋งฅ๋ถํ๋ก
- baekjoon
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก์ด๋์์ฌ
- dfs
- Macbook pro 2012 mid 13
- ๋งฅ๋ถ
- ์๊ฐ๊ต์ฒด
- ์คํ
- Golang
- ๋ถํ ์ ๋ณต
- java
- ๋ฐฑ์ค
- go
- dp
- ํด์๋งต
- ๋ชฝ๊ณ ๋๋น
- ์๋ฐ
- Algorithm
- BOJ
- Total
- Today
- Yesterday