dev/algorithm
BOJ / 6603λ² / λ‘λ [Go][Python3]
crscnt
2021. 3. 22. 21:00
π©π»π» λ¬Έμ
6603λ²: λ‘λ
μ λ ₯μ μ¬λ¬ κ°μ ν μ€νΈ μΌμ΄μ€λ‘ μ΄λ£¨μ΄μ Έ μλ€. κ° ν μ€νΈ μΌμ΄μ€λ ν μ€λ‘ μ΄λ£¨μ΄μ Έ μλ€. 첫 λ²μ§Έ μλ k (6 < k < 13)μ΄κ³ , λ€μ kκ° μλ μ§ν© Sμ ν¬ν¨λλ μμ΄λ€. Sμ μμλ μ€λ¦μ°¨μμΌλ‘
www.acmicpc.net
βπ» νμ΄
π¨ Go
// https://www.acmicpc.net/problem/6603
package main
import (
"bufio"
"fmt"
"math/bits"
"os"
"sort"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
index := 0
for {
var k int
fmt.Fscanf(reader, "%d ", &k)
if k == 0 {
break
}
if index != 0 {
fmt.Fprintln(writer, "")
}
index++
var s = make([]int, k)
for i := 0; i < k; i++ {
fmt.Fscanf(reader, "%d ", &s[i])
}
combinations := Combinations(s, 6)
sort.Slice(combinations[:], func(i, j int) bool {
for x := range combinations[i] {
if combinations[i][x] == combinations[j][x] {
continue
}
return combinations[i][x] < combinations[j][x]
}
return false
})
for i := 0; i < len(combinations); i++ {
combiStr := fmt.Sprintf("%v", combinations[i])
combiStr = combiStr[1 : len(combiStr)-1]
fmt.Fprintln(writer, combiStr)
}
}
}
func Combinations(set []int, n int) (subsets [][]int) {
length := uint(len(set))
if n > len(set) {
n = len(set)
}
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
if n > 0 && bits.OnesCount(uint(subsetBits)) != n {
continue
}
var subset []int
for object := uint(0); object < length; object++ {
if (subsetBits>>object)&1 == 1 {
subset = append(subset, set[object])
}
}
subsets = append(subsets, subset)
}
return subsets
}
π¨ Python3
# https://www.acmicpc.net/problem/6603
import sys
from itertools import combinations
if __name__ == "__main__":
index = 0
while True:
inputs = list(map(int, sys.stdin.readline().split()))
if inputs[0] == 0:
break
if index != 0:
print()
index += 1
combi = list(combinations(inputs[1:], 6))
for i, c in enumerate(combi):
print(*list(c))
728x90