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

N๊ณผ M (3) ๋ฌธ์ œ์—์„œ ํ•œ๊ฐ€์ง€ ์˜ˆ์™ธ๋งŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ํ’€๋ฆฐ๋‹ค. ๋ฐ”๋กœ ์ง์ „์— ๊ณ ๋ฅธ ์ˆซ์ž๋ณด๋‹ค ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ์ž‘์„ ๊ฒฝ์šฐ(= ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ) ํ•ด๋‹น ์ˆซ์ž๋Š” ๊ฑด๋„ˆ๋›ด๋‹ค.

๋ฌธ์ œ

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


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

Swift

let input = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = input[0]
let m = input[1]
var picked = [String]()

/*
 n: ์ „์ฒด ์›์†Œ์˜ ์ˆ˜
 picked: ์ง€๊ธˆ๊นŒ์ง€ ๊ณ ๋ฅธ ์›์†Œ๋“ค์˜ ๋ฒˆํ˜ธ
 toPick: ๋” ๊ณ ๋ฅผ ์›์†Œ์˜ ์ˆ˜
 ์ผ ๋•Œ, ์•ž์œผ๋กœ toPick๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ณ ๋ฅด๋Š” ๋ชจ๋“  ๋ฐฉ๋ฒ•์„ ์ถœ๋ ฅํ•œ๋‹ค
 */
func pick(n: Int, toPick: Int) {
    // ๊ธฐ์ € ์‚ฌ๋ก€: ๋” ๊ณ ๋ฅผ ์›์†Œ๊ฐ€ ์—†์„ ๋•Œ ๊ณ ๋ฅธ ์›์†Œ๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค.
    if toPick == 0 {
        print(picked.joined(separator: " "))
        return
    }
    // ์›์†Œ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅธ๋‹ค.
    for next in 1...n {
        // ๋ฐ”๋กœ ์ง์ „์˜ ๊ณ ๋ฅธ ์ˆ˜๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ฑด๋„ˆ๋›ด๋‹ค.
        if let last = picked.last {
            if Int(last)! > next {
                continue
            }
        }
        picked.append(String(next))
        pick(n: n, toPick: toPick - 1)
        picked.removeLast()
    }
}

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