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

04:23

N๊ณผ M (1) ~ (5) ๋ฌธ์ œ๋Š” ์ข…๋งŒ๋ถ์—์„œ ๋‚˜์˜จ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค. (1)์„ ํ‘ผ ์ฝ”๋“œ๋กœ (2)๋ฅผ ํ’€๊ณ , (3)์„ ํ’€๊ณ … ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฒ ์ด์Šค ์ฝ”๋“œ์—์„œ ์ „๋ถ€ ์ง€์šฐ๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ํ’€์ง€ ์•Š๊ณ  ์‘์šฉํ•ด๋‚˜๊ฐ”๋Š”๋ฐ, (6)์€ ์•„์˜ˆ ๋ฒ ์ด์Šค ์ฝ”๋“œ๋ฅผ ์ง€์šฐ๊ณ  ํ’€์—ˆ๋‹ค.

๋ฌธ์ œ

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


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

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]()
/*
 n: ์ „์ฒด ์›์†Œ์˜ ์ˆ˜
 toPick: ๋” ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜
 */
func pick(n: Int, toPick: Int, start: Int) {
    // ๊ธฐ์ € ์‚ฌ๋ก€: ๋” ์ด์ƒ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์—†์œผ๋ฉด ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒ
    if toPick == 0 {
        print(picked.joined(separator: " "))
        return
    }
    for i in start..<n {
        picked.append(String(nums[i]))
        pick(n: n, toPick: toPick - 1, start: i+1)
        picked.removeLast()
    }
}

pick(n: n, toPick: m, start: 0)
๋ฐ˜์‘ํ˜•