Ejemplo n.º 1
0
 /// <summary>
 /// 排列递归方法。
 /// </summary>
 /// <param name="array">元素数组。</param>
 /// <param name="ii">已循环到的元素数组的索引。</param>
 /// <param name="created">排列完成一组元素后的回调函数。</param>
 /// <param name="userState">传递用户参数。</param>
 private static void Permutate(IList <T> array, int ii, CreatedCallback created, object userState)
 {
     if (ii == array.Count - 1)
     {
         T[] result = new T[array.Count];
         array.CopyTo(result, 0);
         created?.Invoke(result, userState);
     }
     else
     {
         for (int i = ii; i < array.Count; i++)
         {
             if (i == ii)
             {
                 Permutate(array, ii + 1, created, userState);
             }
             else
             {
                 Swap(array, i, ii);
                 Permutate(array, ii + 1, created, userState);
                 Swap(array, i, ii);
             }
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 组合递归方法。
 /// </summary>
 /// <param name="array">元素数组。</param>
 /// <param name="m">指定选择的元素数量。</param>
 /// <param name="ii">循环到的主要分段元素数组的索引。</param>
 /// <param name="jj">循环到的盈余分段元素数组的索引。</param>
 /// <param name="created">组合完成一组元素后的回调函数。</param>
 /// <param name="userState">传递用户参数。</param>
 private static void Combine(IList <T> array, int m, int ii, int jj, CreatedCallback created, object userState)
 {
     for (int i = ii; i < m; i++)
     {
         for (int j = jj; j < array.Count; j++)
         {
             Swap(array, i, j);
             Combine(array, m, i + 1, j + 1, created, userState);
             Swap(array, i, j);
         }
     }
     T[] result = new T[m];
     for (int i = 0; i < result.Length; i++)
     {
         result[i] = array[i];
     }
     created?.Invoke(result, userState);
 }