亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解C#的排列組合

 更新時間:2016年11月21日 11:22:28   作者:逆心  
本文詳細介紹了C#中的排列組合以及具體實現(xiàn)代碼,如下所示,希望對大家有所幫助

排列組合的概念

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

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

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

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

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

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>
 /// 遞歸算法求數(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">起始標號</param>
 /// <param name="endIndex">結(jié)束標號</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>
 /// 求從起始標號到結(jié)束標號的排列,其余元素不變
 /// </summary>
 /// <param name="t">所求數(shù)組</param>
 /// <param name="startIndex">起始標號</param>
 /// <param name="endIndex">結(jié)束標號</param>
 /// <returns>從起始標號到結(jié)束標號排列的范型</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>
 /// 返回數(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個元素的排列
 /// </summary>
 /// <param name="t">所求數(shù)組</param>
 /// <param name="n">元素個數(shù)</param>
 /// <returns>數(shù)組中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>
 /// 求數(shù)組中n個元素的組合
 /// </summary>
 /// <param name="t">所求數(shù)組</param>
 /// <param name="n">元素個數(shù)</param>
 /// <returns>數(shù)組中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個數(shù)里任意3個數(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個數(shù)取3個的任意排列

 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)容,希望對大家有所幫助!

相關(guān)文章

  • Unity實現(xiàn)QQ列表折疊菜單

    Unity實現(xiàn)QQ列表折疊菜單

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)QQ列表折疊菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#使用默認瀏覽器打開網(wǎng)頁的方法

    C#使用默認瀏覽器打開網(wǎng)頁的方法

    這篇文章主要介紹了C#使用默認瀏覽器打開網(wǎng)頁的方法,涉及C#操作瀏覽器屬性的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#遍歷操作系統(tǒng)下所有驅(qū)動器的方法

    C#遍歷操作系統(tǒng)下所有驅(qū)動器的方法

    這篇文章主要介紹了C#遍歷操作系統(tǒng)下所有驅(qū)動器的方法,涉及C#中DriveInfo類GetDrivers方法的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#對list列表進行隨機排序的方法

    C#對list列表進行隨機排序的方法

    這篇文章主要介紹了C#對list列表進行隨機排序的方法,涉及C#操作list列表的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C# 遍歷datatable字段名和value的案例

    C# 遍歷datatable字段名和value的案例

    這篇文章主要介紹了C# 遍歷datatable字段名和value的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#畫筆Pen保存和恢復圖形對象的設(shè)置方法

    C#畫筆Pen保存和恢復圖形對象的設(shè)置方法

    這篇文章主要介紹了C#畫筆Pen保存和恢復圖形對象的設(shè)置方法,實例分析了畫筆的保存save及恢復屬性Restore的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • C#開發(fā)微信門戶及應用(2) 微信消息處理和應答

    C#開發(fā)微信門戶及應用(2) 微信消息處理和應答

    文章主要為大家詳細介紹了C#開發(fā)微信門戶及應用第二篇,微信消息處理和應答,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C# 實現(xiàn)QQ式截圖功能實例代碼

    C# 實現(xiàn)QQ式截圖功能實例代碼

    本篇文章主要介紹了C# 實現(xiàn)QQ式截圖功能實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • C#訪問命令行的兩種方法

    C#訪問命令行的兩種方法

    這篇文章主要介紹了C#訪問命令行的兩種方法,實例分析了C#操作命令行的兩種常用技巧,需要的朋友可以參考下
    2015-06-06
  • XAML如何獲取元素的位置

    XAML如何獲取元素的位置

    這篇文章主要為大家詳細介紹了XAML如何獲取元素的位置,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評論