欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

詳解C#的排列組合

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

排列組合的概念

排列:從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#教程

下一篇:C#編程總結(jié)(六)詳解異步編程

本文標(biāo)題:詳解C#的排列組合

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6153.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有