https://www.acmicpc.net/problem/2309
swift에는 combination이 없어서 직접 구현해야한다. combination함수를 구현하고 9C7의 경우를 구하면 쉽게 풀 수 있다.
func combination<T>(_ elements: [T], _ k: Int) -> [[T]] {
var result = [[T]]()
func combi(_ index: Int, _ now: [T]) {
if now.count == k {
result.append(now)
return
}
for i in index..<elements.count {
combi(i + 1, now + [elements[i]])
}
}
combi(0, [])
return result
}
var sum = [Int]()
for _ in 0..<9 {
let height = Int(readLine()!) ?? 0
sum.append(height)
}
sum.sort()
for i in combination(sum,7) {
if i.reduce(0, +) == 100 {
for j in i {
print(j)
}
break
}
}