詳解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("");
}

以上就是本文的全部內(nèi)容,希望對(duì)大家有所幫助!
相關(guān)文章
C#遍歷操作系統(tǒng)下所有驅(qū)動(dòng)器的方法
這篇文章主要介紹了C#遍歷操作系統(tǒng)下所有驅(qū)動(dòng)器的方法,涉及C#中DriveInfo類GetDrivers方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#對(duì)list列表進(jìn)行隨機(jī)排序的方法
這篇文章主要介紹了C#對(duì)list列表進(jìn)行隨機(jī)排序的方法,涉及C#操作list列表的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#畫筆Pen保存和恢復(fù)圖形對(duì)象的設(shè)置方法
這篇文章主要介紹了C#畫筆Pen保存和恢復(fù)圖形對(duì)象的設(shè)置方法,實(shí)例分析了畫筆的保存save及恢復(fù)屬性Restore的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06
C#開發(fā)微信門戶及應(yīng)用(2) 微信消息處理和應(yīng)答
文章主要為大家詳細(xì)介紹了C#開發(fā)微信門戶及應(yīng)用第二篇,微信消息處理和應(yīng)答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼
本篇文章主要介紹了C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

