国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

詳解C#的排列組合

2020-01-24 00:56:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

排列組合的概念

排列:從n個(gè)不同元素中取出m(m≤n)個(gè)元素,按照一定的順序排成一列,叫做從n個(gè)元素中取出m個(gè)元素的一個(gè)排列(Arrangement)。

組合:從m個(gè)不同的元素中,任取n(n≤m)個(gè)元素為一組,叫作從m個(gè)不同元素中取出n個(gè)元素的一個(gè)組合。

排列組合實(shí)現(xiàn)代碼

上一個(gè)項(xiàng)目做的一個(gè)水路的路徑規(guī)劃時(shí),用到了排列的數(shù)據(jù)結(jié)構(gòu)。求任意N個(gè)點(diǎn)里M個(gè)點(diǎn)的不同順序的組合個(gè)數(shù)。

這樣求最優(yōu)路徑。下面貼一段不知道哪里找的排列組合的算法。

public class PermutationAndCombination<T> { /// <summary> /// 交換兩個(gè)變量 /// </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> /// 遞歸算法求數(shù)組的組合(私有成員) /// </summary> /// <param name="list">返回的范型</param> /// <param name="t">所求數(shù)組</param> /// <param name="n">輔助變量</param> /// <param name="m">輔助變量</param> /// <param name="b">輔助數(shù)組</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">所求數(shù)組</param> /// <param name="startIndex">起始標(biāo)號(hào)</param> /// <param name="endIndex">結(jié)束標(biāo)號(hào)</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> /// 求從起始標(biāo)號(hào)到結(jié)束標(biāo)號(hào)的排列,其余元素不變 /// </summary> /// <param name="t">所求數(shù)組</param> /// <param name="startIndex">起始標(biāo)號(hào)</param> /// <param name="endIndex">結(jié)束標(biāo)號(hào)</param> /// <returns>從起始標(biāo)號(hào)到結(jié)束標(biāo)號(hào)排列的范型</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> /// 返回?cái)?shù)組所有元素的全排列 /// </summary> /// <param name="t">所求數(shù)組</param> /// <returns>全排列的范型</returns> public static List<T[]> GetPermutation(T[] t) { return GetPermutation(t, 0, t.Length - 1); } /// <summary> /// 求數(shù)組中n個(gè)元素的排列 /// </summary> /// <param name="t">所求數(shù)組</param> /// <param name="n">元素個(gè)數(shù)</param> /// <returns>數(shù)組中n個(gè)元素的排列</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> /// 求數(shù)組中n個(gè)元素的組合 /// </summary> /// <param name="t">所求數(shù)組</param> /// <param name="n">元素個(gè)數(shù)</param> /// <returns>數(shù)組中n個(gè)元素的組合的范型</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個(gè)數(shù)里任意3個(gè)數(shù)的組合

 static void Main(string[] args) { int[] IntArr = new int[] { 1, 2, 3, 4, 5 }; //整型數(shù)組 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個(gè)數(shù)取3個(gè)的任意排列

 int[] IntArr = new int[] { 1, 2, 3, 4, 5 }; //整型數(shù)組 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(""); }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家有所幫助!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 延安市| 安达市| 正宁县| 襄垣县| 兴业县| 怀宁县| 马公市| 雷州市| 和林格尔县| 富宁县| 鄂尔多斯市| 阳山县| 民和| 南岸区| 两当县| 湘潭市| 梅州市| 册亨县| 固始县| 阿拉善右旗| 漳平市| 河间市| 松潘县| 莱西市| 略阳县| 宁城县| 威海市| 峨边| 镇赉县| 博罗县| 常山县| 温宿县| 疏勒县| 临安市| 平和县| 麦盖提县| 阿坝| 谢通门县| 平邑县| 临汾市| 喀喇沁旗|