热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

C#算法排列类

发布时间:2010年01月23日点击数: 未知
  1. // 调用方法如下: 
  2. // 
  3. // 1.GetPermutation(T[], startIndex, endIndex) 
  4. // 返回从startIndex到endIndex的排列 
  5. // 
  6. // 2.GetPermutation(T[]) 
  7. // 返回数组所有元素的全排列 
  8. // 
  9. // 版本历史: 
  10. // V0.1 2010-01-20 摘要:首次创建  
  11. // 
  12. //----------------------------------------------------------------------------- 
  13.  
  14. using System; 
  15. using System.Collections.Generic; 
  16.  
  17. namespace Arithmetic 
  18.     public class Permutation<T> 
  19.     { 
  20.         /// <summary> 
  21.         /// 交换两个变量 
  22.         /// </summary> 
  23.         /// <param name="a">变量1</param> 
  24.         /// <param name="b">变量2</param> 
  25.         public static void Swap(ref T a, ref T b) 
  26.         { 
  27.             T temp = a; 
  28.             a = b; 
  29.             b = temp; 
  30.         } 
  31.  
  32.         /// <summary> 
  33.         /// 递归算法求排列(私有成员) 
  34.         /// </summary> 
  35.         /// <param name="list">返回的列表</param> 
  36.         /// <param name="t">原始数组</param> 
  37.         /// <param name="startIndex">起始标号</param> 
  38.         /// <param name="endIndex">结束标号</param> 
  39.         private static void GetPermutation(ref List<T[]> list, T[] t, int startIndex, int endIndex) 
  40.         { 
  41.             if (startIndex == endIndex) 
  42.             { 
  43.                 if (list == null
  44.                 { 
  45.                     list = new List<T[]>(); 
  46.                 } 
  47.                 T[] temp = new T[t.Length]; 
  48.                 t.CopyTo(temp, 0); 
  49.                 list.Add(temp); 
  50.             } 
  51.             else 
  52.             { 
  53.                 for (int i = startIndex; i <= endIndex; i++) 
  54.                 { 
  55.                     Swap(ref t[startIndex], ref t[i]); 
  56.                     GetPermutation(ref list, t, startIndex + 1, endIndex); 
  57.                     Swap(ref t[startIndex], ref t[i]); 
  58.                 } 
  59.             } 
  60.         } 
  61.  
  62.         /// <summary> 
  63.         /// 求数组的排列 
  64.         /// </summary> 
  65.         /// <param name="t">原始数组</param> 
  66.         /// <param name="startIndex">起始标号</param> 
  67.         /// <param name="endIndex">结束标号</param> 
  68.         /// <returns>从起始标号到结束标号的排列</returns> 
  69.         public static List<T[]> GetPermutation(T[] t, int startIndex, int endIndex) 
  70.         { 
  71.             if (startIndex < 0 || endIndex > t.Length - 1) 
  72.             { 
  73.                 return null
  74.             } 
  75.             List<T[]> list = new List<T[]>(); 
  76.             GetPermutation(ref list, t, startIndex, endIndex); 
  77.             return list; 
  78.         } 
  79.  
  80.         /// <summary> 
  81.         /// 求数组的排列 
  82.         /// </summary> 
  83.         /// <param name="t">原始数组</param> 
  84.         /// <returns>全排列</returns> 
  85.         public static List<T[]> GetPermutation(T[] t) 
  86.         { 
  87.             return GetPermutation(t, 0, t.Length - 1); 
  88.         } 
  89.     } 

调用: 

  1. int[] arr = new int[5]; 
  2. for (int i = 0; i < arr.Length; i++) 
  3.     arr[i] = i + 1; 
  4. List<int[]> list = Arithmetic.Permutation<int>.GetPermutation(arr); 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作