Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1158๋ฒˆ - ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

๊ฐ์ž ๐Ÿฅ” 2022. 2. 5. 15:21
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1158

 

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

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

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

  • idx๋ฅผ pop, push๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์ €๋ ‡๊ฒŒ ๋ง์…ˆ๊ณผ count๋ฅผ ํ™œ์šฉํ•œ ๋‚˜๋ˆ—์…ˆ์œผ๋กœ ๊ทœ์น™์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด ๋ถ€๋ถ„์—์„œ ์กฐ๊ธˆ ๋ง‰ํ˜”์ง€๋งŒ, ๊ทœ์น™์„ ์ฐพ์œผ๋‹ˆ ์™„์ „ํžˆ ์ดํ•ด๊ฐ€ ๊ฐ”๋‹ค. 
import Foundation

let input = readLine()!.split(separator: " ").map { Int($0)! }
var nums: [Int] = []
var answer: [Int] = []
var idx = input[1] - 1

for i in 1..<input[0]+1 {
    nums.append(i)
}

while true {
    answer.append(nums.remove(at: idx))
    if nums.isEmpty { break }
    idx = (idx + input[1]-1) % nums.count
}


print("<" + answer.map({String($0)}).joined(separator: ", ") + ">")
๋ฐ˜์‘ํ˜•