๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

06:47

์ด ๋ฌธ์ œ๋Š” ์›๋ž˜ toPick == 0์ผ ๋•Œ picked๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ๊ทธ๋ž˜์„œ ํ˜น์‹œ ํ•˜๋Š” ๋งˆ์Œ์—, ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•ด์„œ ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ–ˆ๋”๋‹ˆ ํ†ต๊ณผ.


๋ฌธ์ œ

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


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let input = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = input[0]
let m = input[1]
let nums = readLine()!.split(separator: " ").map { Int(String($0))! }.sorted()
var picked = [String]()
var result = ""
/*
 n: ์ „์ฒด ์›์†Œ์˜ ์ˆ˜
 toPick: ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜
 */
func pick(n: Int, toPick: Int) {
    // ๊ธฐ์ € ์‚ฌ๋ก€: ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜๊ฐ€ ์—†์œผ๋ฉด ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒ
    if toPick == 0 {
        result += picked.joined(separator: " ") + "\n"
        return
    }

    // ์ˆ˜๋ฅผ ๊ณ ๋ฅธ๋‹ค. ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณจ๋ผ๋„ ๋œ๋‹ค.
    for i in 0..<n {
        picked.append(String(nums[i]))
        pick(n: n, toPick: toPick - 1)
        picked.removeLast()
    }
}

pick(n: n, toPick: m)

print(result, terminator: "")
๋ฐ˜์‘ํ˜•