Ejemplo n.º 1
0
        ///// <summary>
        ///// 求从起始标号到结束标号的排列,其余元素不变
        ///// </summary>
        ///// <param name="t">所求数组</param>
        ///// <param name="startIndex">起始标号</param>
        ///// <param name="endIndex">结束标号</param>
        ///// <returns>从起始标号到结束标号排列的范型</returns>
        //public static List<List<T>> GetPermutation(List<T> t, int startIndex, int endIndex)
        //{
        //    if (startIndex < 0 || endIndex > t.Count - 1)
        //    {
        //        return null;
        //    }
        //    List<List<T>> list = new List<List<T>>();
        //    GetPermutation(ref list, t, startIndex, endIndex);
        //    return list;
        //}

        ///// <summary>
        ///// 返回数组所有元素的全排列
        ///// </summary>
        ///// <param name="t">所求数组</param>
        ///// <returns>全排列的范型</returns>
        //public static List<List<T>> GetPermutationPailie(List<T> t) //where T : new()
        //{
        //    return GetPermutation(t, 0, t.Count - 1);
        //}

        ///// <summary>
        ///// 求数组中n个元素的排列
        ///// </summary>
        ///// <param name="t">所求数组</param>
        ///// <param name="n">元素个数</param>
        ///// <returns>数组中n个元素的排列</returns>
        //public static List<List<T>> GetPermutation(List<T> t, int n)
        //{
        //    if (n > t.Count)
        //    {
        //        return null;
        //    }
        //    List<List<T>> list = new List<List<T>>();
        //    List<List<T>> c = GetCombination(t, n, 一个缓冲);
        //    for (int i = 0; i < c.Count; i++)
        //    {
        //        List<List<T>> l = new List<List<T>>();
        //        GetPermutation(ref l, c[i], 0, n - 1);
        //        list.AddRange(l);
        //    }
        //    return list;
        //}

        /// <summary>
        /// 求数组中n个元素的组合
        /// </summary>
        /// <param name="t">所求数组</param>
        /// <param name="n">元素个数</param>
        /// <returns>数组中n个元素的组合的范型</returns>
        public static void GetCombination(List <T> t, int n, PC_CombTable 一个缓冲)
        {
            if (t.Count < n)
            {
                return;
            }
            List <int> temp = new List <int>();

            for (int lvii = 0; lvii < n; lvii++)
            {
                temp.Add(0);
            }
            //List<List<T>> list = new List<List<T>>();
            GetCombination(t, t.Count, n, temp, n, 一个缓冲);
            //return list;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 递归算法求数组的组合(私有成员)
 /// </summary>
 /// <param name="list">返回的范型</param>
 /// <param name="t">所求数组</param>
 /// <param name="n">辅助变量</param>
 /// <param name="m">辅助变量</param>
 /// <param name="b">辅助数组</param>
 /// <param name="M">辅助变量M</param>
 private static void GetCombination(List <T> t, int n, int m, List <int> b, int M, PC_CombTable 一个缓冲)
 {
     for (int i = n; i >= m; i--)
     {
         b[m - 1] = i - 1;
         if (m > 1)
         {
             GetCombination(t, i - 1, m - 1, b, M, 一个缓冲);
         }
         else
         {
             //if (list == null)
             //{
             //    list = new List<List<T>>();
             //}
             List <T> temp = new List <T>();
             for (int j = 0; j < b.Count; j++)
             {
                 temp.Add(t[b[j]]);
             }
             if (typeof(T) == typeof(int))
             {
                 List <int> ttt = new List <int>();
                 foreach (T lvt in temp)
                 {
                     ttt.Add(Convert.ToInt32(lvt));
                 }
                 while (!一个缓冲.Enque(ttt))
                 {
                     Thread.Sleep(200);
                 }
             }
         }
     }
 }