排列組合的概念
排列:從n個不同元素中取出m(m≤n)個元素,按照一定的順序排成一列,叫做從n個元素中取出m個元素的一個排列(Arrangement)。
組合:從m個不同的元素中,任取n(n≤m)個元素為一組,叫作從m個不同元素中取出n個元素的一個組合。
排列組合實現代碼
上一個項目做的一個水路的路徑規劃時,用到了排列的資料結構。求任意N個點裡M個點的不同順序的組合個數。
這樣求最優路徑。下面貼一段不知道哪裡找的排列組合的演算法。
public class PermutationAndCombination<T> { /// <summary> /// 交換兩個變數 /// </summary> /// <param name="a">變數1</param> /// <param name="b">變數2</param> public static void Swap(ref T a, ref T b) { T temp = a; a = b; b = temp; } /// <summary> /// 遞迴演算法求數組的組合(私人成員) /// </summary> /// <param name="list">返回的範型</param> /// <param name="t">所求數組</param> /// <param name="n">輔助變數</param> /// <param name="m">輔助變數</param> /// <param name="b">輔助數組</param> /// <param name="M">輔助變數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> /// 遞迴演算法求排列(私人成員) /// </summary> /// <param name="list">返回的列表</param> /// <param name="t">所求數組</param> /// <param name="startIndex">起始標號</param> /// <param name="endIndex">結束標號</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> /// 求從起始標號到結束標號的排列,其餘元素不變 /// </summary> /// <param name="t">所求數組</param> /// <param name="startIndex">起始標號</param> /// <param name="endIndex">結束標號</param> /// <returns>從起始標號到結束標號排列的範型</returns> public static List<T[]> GetPermutation(T[] t, int 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> /// 返回數組所有元素的全排列 /// </summary> /// <param name="t">所求數組</param> /// <returns>全排列的範型</returns> public static List<T[]> GetPermutation(T[] t) { return GetPermutation(t, 0, t.Length - 1); } /// <summary> /// 求數組中n個元素的排列 /// </summary> /// <param name="t">所求數組</param> /// <param name="n">元素個數</param> /// <returns>數組中n個元素的排列</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> /// 求數組中n個元素的組合 /// </summary> /// <param name="t">所求數組</param> /// <param name="n">元素個數</param> /// <returns>數組中n個元素的組合的範型</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; } }
求組合:求5個數裡任意3個數的組合
static void Main(string[] args){int[] IntArr = new int[] { 1, 2, 3, 4, 5 }; //整型數組List<int[]> ListCombination = PermutationAndCombination<int>.GetCombination(IntArr, 3); //求全部的3-3組合foreach(int[] arr in ListCombination){foreach(int item in arr){Console.Write(item + " ");}Console.WriteLine("");}Console.ReadKey();}
求排列:5個數取3個的任意排列
i
nt[] IntArr = new int[] { 1, 2, 3, 4, 5 }; //整型數組List<int[]> ListCombination = PermutationAndCombination<int>.GetPermutation(IntArr, 3); //求全部的5取3排列foreach(int[] arr in ListCombination){foreach(int item in arr){Console.Write(item + " ");}Console.WriteLine("");}
以上就是詳解C#的排列組合的內容,更多相關文章請關注topic.alibabacloud.com(www.php.cn)!