A detailed description of C # permutations and combinations

Source: Internet
Author: User
The concept of permutation combinations

Arrange: Remove m (m≤n) elements from n different elements, in a certain order, called a permutation of m elements from n elements (arrangement).

Combination: from m different elements, any n (n≤m) element is a group called a combination of n elements taken from m different elements.

Permutation combination Implementation code

The previous project did a waterway path planning when using the arranged data structure. The number of combinations of the different sequences of M points in any n points.

The optimal path is thus obtained. The following is an algorithm that does not know where to look for permutation combinations.

public class Permutationandcombination<t> {//<summary>///Exchange two variables//</summary>//<param name= " A "> Variable 1</param>//<param name=" B "> variable 2</param> public static void Swap (Ref t A, ref T B) {T temp = a ; A = b; b = temp; }///<summary>///recursive algorithm for array combination (private Member)///</summary>//<param name= "List" > Return paradigm </param>//< param name= "T" > Array </param>//<param name= "n" > Auxiliary variable </param>//<param name= "M" > Auxiliary variable </ param>//<param name= "B" > Auxiliary array </param>///<param Name= "M" > Auxiliary variable m</param> private static  void Getcombination (ref list<t[]> List, t[] T, int n, int m, int[] b, int m) {for (int i = n; i >= m; i--) {b[m -1] = i-1; if (M > 1) {getcombination (ref list, T, I-1, m-1, B, M);} else {if (list = = null) {list = new list<t[]> () ; } t[] temp = new T[m]; for (int j = 0; J < B.length; J + +) {Temp[j] = T[b[j]];} list. ADD (temp); }}}///<summary&Gt Recursive algorithm permutation (private member)///</summary>//<param name= "List" > returned list </param>//<param name= "T" > the array to be evaluated and lt;/param>//<param name= "StartIndex" > Starting label </param>//<param name= "EndIndex" > End label </param  > private static void Getpermutation (ref list<t[]> List, t[] T, int startIndex, int endIndex) {if (StartIndex = = EndIndex) {if (list = = null) {list = new list<t[]> ();} t[] temp = new T[t.length]; T.copyto (temp, 0); List. ADD (temp); } else {for (int i = startIndex; I <= endIndex; i++) {Swap (ref T[startindex], ref t[i]); Getpermutation (ref list, T, StartIndex + 1, endIndex); Swap (ref T[startindex], ref t[i]); }}}///<summary>//For the arrangement from the starting label to the end label, the remaining elements are unchanged///</summary>//<param name= "T" > The array </param>// <param name= "StartIndex" > Start label </param>//<param name= "EndIndex" > End label </param>//< Returns> type </returns> public static list<t[]> getpermutation (t[] T, I, from the starting label to the end label arrangementNT StartIndex, int endIndex) {if (StartIndex < 0 | | EndIndex > T.LENGTH-1) {return null;} list<t[]> list = new list<t[]> (); Getpermutation (ref list, T, StartIndex, EndIndex); return list; }///<summary>///Returns the full arrangement of all elements of the array///</summary>//<param name= "T" > The array </param>//<returns&  gt; full-aligned paradigm </returns> public static list<t[]> getpermutation (t[] t) {return getpermutation (T, 0, t.length-1); }///<summary>//To arrange the arrangement of n elements in the array///</summary>//<param name= "T" > The array </param>//<param Nam E= "n" > number of Elements </param>//<returns> arrangement of n elements in arrays </returns> public static list<t[]> Getpermutation (t[] T, int n) {if (n > T.length) {return null;} list<t[]> list = new list<t[]> (); list<t[]> C = getcombination (T, N); for (int i = 0; i < C.count; i++) {list<t[]> L = new list<t[]> (); Getpermutation (ref L, C[i], 0, n-1); List. AddRange (l); } return list; }//<summary>////Find a combination of n elements in an array///</summary>//<param name= "T" > Array </param>//<param name= "n" > Number of elements </param>//<returns> combination of n elements in an array of paradigms </returns> public static list<t[]> getcombination (t[] T, int n) {if (T.length < n) {return null;} int[] temp = new Int[n]; list<t[]> list = new list<t[]> (); Getcombination (ref list, T, T.length, N, temp, n); return list; } }

Combinatorial: Find a combination of any 3 numbers in 5 numbers

static void Main (string[] args) {int[] intarr = new int[] {1, 2, 3, 4, 5};//integer array list<int[]> listcombination = Perm Utationandcombination<int>. Getcombination (Intarr, 3); For all 3-3 combinations of foreach (int[] arr in listcombination) {foreach (int item in arr) {Console.Write (item + "");} Console.WriteLine ("");} Console.readkey ();}

Arrange: 5 numbers to take 3 arbitrary arrangement

I

nt[] intarr = new int[] {1, 2, 3, 4, 5}; Integer array list<int[]> listcombination = Permutationandcombination<int>. Getpermutation (Intarr, 3); Ask for all 5 fetch 3 to arrange foreach (int[] arr in listcombination) {foreach (int item in arr) {Console.Write (item + "");} Console.WriteLine ("");}

The above is a detailed description of C # permutation of the content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.