2025-08-22 21:38:12 +09:00

24 lines
549 B
Swift

func dfs(_ n:Int, _ m:Int, _ now:Int, _ result:inout [Int]) {
if result.count == m {
for n in result {
print(n, terminator: " ")
}
print()
return
}
for next in now...n {
result.append(next)
dfs(n,m,next,&result)
_ = result.popLast()
}
}
if let input = readLine(),
let nums:[Int] = input.split(separator: " ").compactMap({Int($0)}) as? [Int],
nums.count==2 {
let N:Int = nums[0]
let M:Int = nums[1]
var result:[Int] = []
dfs(N,M,1,&result)
}