詳解C#的排列組合
排列組合的概念
排列:從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ì)大家有所幫助!
上一篇:C#在winform中實(shí)現(xiàn)數(shù)據(jù)增刪改查等功能
欄 目:C#教程
本文標(biāo)題:詳解C#的排列組合
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6153.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法