diff --git a/LC1/.cph/.4.go_ca53fb91dfb9dd7a45a3e69867f3edcc.prob b/LC1/.cph/.4.go_ca53fb91dfb9dd7a45a3e69867f3edcc.prob new file mode 100644 index 0000000..27cff18 --- /dev/null +++ b/LC1/.cph/.4.go_ca53fb91dfb9dd7a45a3e69867f3edcc.prob @@ -0,0 +1 @@ +{"name":"Local: 4","url":"/home/wonder/code/algrithm/LC1/4.go","tests":[{"id":1780037619072,"input":"5 1","output":"1 \n2 \n3 \n4 \n5 \n"},{"id":1780037766692,"input":"5 3","output":"1 2 3\n1 2 4\n1 2 5\n1 3 4\n1 3 5\n1 4 5\n2 3 4\n2 3 5\n2 4 5\n3 4 5"},{"id":1780037945579,"input":"6 2","output":"1 2 \n1 3 \n1 4 \n1 5 \n1 6 \n2 3 \n2 4 \n2 5 \n2 6 \n3 4 \n3 5 \n3 6 \n4 5 \n4 6 \n5 6 \n"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/home/wonder/code/algrithm/LC1/4.go","group":"local","local":true} \ No newline at end of file diff --git a/LC1/4.go b/LC1/4.go new file mode 100644 index 0000000..305d18d --- /dev/null +++ b/LC1/4.go @@ -0,0 +1,34 @@ +//go:build ignore + +// 组合的输出 从 n 个元素(1-n)中选取 r 个数 +package main + +import "fmt" + +var ( + n int + r int + path []int +) + +func dfs(x int) { + // Return + if len(path) >= r { + for _, v := range path { + fmt.Printf("%d ", v) + } + fmt.Printf("\n") + return + } + // Traverse + for i := x; i <= n; i++ { + path = append(path, i) + dfs(i + 1) + path = path[:len(path)-1] + } +} + +func main() { + fmt.Scan(&n, &r) + dfs(1) +}