Skip to content

组合 #5

@asu126

Description

@asu126
package main
	
import (
	"fmt"
)

func combne(arr []int, m int){
	var length int = len(arr)
	var flag []bool =make([]bool,length)
	for i:=0;i<m;i++{
		flag[i] = true
	}

	var startLocation int =m-1

	for ;startLocation < length; {
		for i:=0;i<len(flag);i++{
			if flag[i] == true{
				fmt.Print(arr[i])
			}
		}
		fmt.Println("--------")

		// change
		flag[startLocation] = false
		flag[startLocation+1] = true
		// 同时将其左边的所有“1”全部移动到数组的最左端
		move(flag, startLocation)

		startLocation = CheckoutOneZero(flag)
	}
 	// print last
	for i:=0;i<len(flag);i++{
			if flag[i] == true{
				fmt.Print(arr[i])
			}
		}

}
func move(b []bool, n int){
	var count int = 0
	for i :=0;i<n;i++{
		if b[i]{
			count++
		}
	}

	for i :=0;i<n;i++{
		if i < count{
			b[i] = true
		}else{
			b[i] = false
		}
	}
}

func CheckoutOneZero(b []bool) int{
	for i:=1;i<len(b);i++{
		if b[i-1] == true && b[i] == false{
			return i-1
		}
	}
	return len(b)
}

func main(){
	arr := []int{1,2,3,4,5}
	combne(arr,3)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions