Permutation combination is a basic mathematical problem, the goal of this program is to output all the combinations of m from n elements.
For example, 2 numbers are taken from [three-in-one], with a total of 3 in combination: [1,2],[1,3],[2,3]. (The combination does not consider the order, that is, [] and [2,1] belong to the same combination)
The idea of this procedure (from other great gods on the Internet):
(1) Create an array of n elements, the value of the array element is 1 to select, and 0 is not selected.
(2) Initialize, set the first M element of the array to 1, indicating that the number of previous m is a combination.
(3) Scan the "10" combination of array element values from left to right, find the first "10" combination and turn it into a "01" combination, and all "1" to the left of the array is moved to the leftmost end of it.
(4) When a cycle does not find a "10" combination, the description gets the last combination, the loop ends.
For example, choose 5 of the 3 combinations:
1 1 1 0 0//1,2,3
1 1 0 1 0//1,2,4
1 0 1 1 0//1,3,4
0 1 1 1 0//2,3,4
1 1 0 0 1//1,2,5
1 0 1 0 1//1,3,5
0 1 1 0 1//2,3,5
1 0 0 1 1//1,4,5
0 1 0 1 1//2,4,5
0 0 1 1 1//3,4,5
Efficiency: 20 elements to take 5, a total of 15,504 results, time consuming about 10ms.
Code implementation:
PackageHuaweiImport("FMT" "Time")/* "Permutation combination problem: N number of M" * *funcTest10base () {nums: = []int{1,2,3,4,5,6,7,8,9,Ten} m: =5Timestart: = time. Now () N: =Len(nums) Indexs: = Zuheresult (n, m) Result: = Findnumsbyindexs (Nums, indexs) Timeend: = time. Now () Fmt. Println ("Count:",Len(result)) Fmt. Println ("Result:", result) fmt. Println ("time Consume:", Timeend.sub (Timestart))//The result is correctRightcount: = Mathzuhe (n, m)ifRightcount = =Len(Result) {FMT. Println ("The results are correct") }Else{FMT. Println ("The result is wrong, the correct result is:", Rightcount)}}//combination algorithm (remove m number from Nums)funcZuheresult (nintMint) [][]int{ifM <1|| M > N {fmt. Println ("illegal argument. Param m must between 1 and Len (nums). ")return[][]int{} }//Save the final result array, the total number is calculated directly by the mathematical formulaResult: = Make([][]int,0, Mathzuhe (n, m))//Save an array of indexes for each combination, 1 for selected, 0 for uncheckedIndexs: = Make([]int, N) forI: =0; I < n; i++ {ifI < m {Indexs[i] =1}Else{Indexs[i] =0} }//First resultresult = AddTo (result, Indexs) for{Find: =false //Each loop will change the first occurrence of 1 0 to 0 1, while moving the left 1 to the left forI: =0; I < n-1; i++ {ifIndexs[i] = =1&& indexs[i+1] ==0{find =trueIndexs[i], Indexs[i+1] =0,1 ifi >1{moveonetoleft (indexs[:i])} result = AddTo (result, Indexs) Break} }//This cycle did not find 1 0, indicating that the last situation has been taken if!find { Break} }returnResult//Ele is copied and added to Arr, returning the new arrayfuncAddTo (arr []int, Ele []int) [][]int{Newele: = Make([]int,Len(Ele))Copy(Newele, ele) arr =Append(arr, Newele)returnArrfuncMoveonetoleft (Leftnums []int) {//Calculate a few 1Sum: =0 forI: =0; I <Len(leftnums); i++ {ifLeftnums[i] = =1{sum++}}//Change the former sum to 1, then change to 0 forI: =0; I <Len(leftnums); i++ {ifI < sum {Leftnums[i] =1}Else{Leftnums[i] =0} }}//Get an array of elements based on the index numbers groupfuncFindnumsbyindexs (Nums []int, Indexs []int) [][]int{if Len(indexs) = =0{return[][]int{}} Result: = Make([][]int,Len(Indexs)) forI, V: =RangeIndexs {line: = Make([]int,0) forJ, V2: =RangeV {ifV2 = =1{line =Append(line, Nums[j])} } Result[i] = line}returnResult
Note: The number of M in n elements can be calculated directly from the mathematical formula, namely:
//Mathematical method to calculate the number of permutations (number of M from N)funcMathpailie (nintMint)int{returnJiecheng (n)/Jiecheng (N-M)}//Mathematical method to calculate the number of combinations (number of M from N)funcMathzuhe (nintMint)int{returnJiecheng (n)/(Jiecheng (n-m) * Jiecheng (m))}//FactorialfuncJiecheng (nint)int{Result: =1 forI: =2; I <= N; i++ {result *= i}returnResult
With this formula, you can simply verify that the results of the above program are correct.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Permutation combination problem: N number of M (Golang implementation)