๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1158
๋ด๊ฐ ํผ ํ์ด
- 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: ", ") + ">")
๋ฐ์ํ
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] (Swift) 17413๋ฒ - ๋จ์ด๋ค์ง๊ธฐ2 (0) | 2022.02.07 |
---|---|
[๋ฐฑ์ค] (Swift) 10866๋ฒ - ๋ฑ(deque) (0) | 2022.02.06 |
[๋ฐฑ์ค] (Swift) 10845๋ฒ - ํ (0) | 2022.02.04 |
[๋ฐฑ์ค] (Swift) 1406๋ฒ - ์๋ํฐ (2) | 2022.01.30 |
[๋ฐฑ์ค] (Swift) 1874๋ฒ - ์คํ ์์ด (0) | 2022.01.28 |