Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 25, 2017 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/509c9f2db909f012cb1c04705370111b to your computer and use it in GitHub Desktop.
Save thinkphp/509c9f2db909f012cb1c04705370111b to your computer and use it in GitHub Desktop.
package main
import ("fmt"
"os"
"strconv")
func generateSubsets(n int) {
arr := make([]int, n)
var i, s int
for s < n {
arr[ n - 1 ]++
for i := len(arr) - 1; i > 0; i-- {
if arr[i] > 1 {
arr[i] -= 2
arr[i - 1] += 1
}
}
for i = 0; i <= n - 1; i++ {
if arr[ i ] != 0 {
fmt.Printf("%d ", (i + 1))
}
}
s = 0
for i = 0; i <= n - 1; i++ {
if arr[ i ] == 1 {
s++
}
}
fmt.Printf("\n")
}
}
func main() {
var n,_ = strconv.Atoi(os.Args[1])
generateSubsets( n )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment