• 全排列和组合算法


    C#中使用的全排列和组合算法:

            /// <summary>
            /// 对数组进行组合操作,选取selectCount个元素进行组合
            /// </summary>
            /// <param name="lsArray">即将进行组合操作的数组</param>
            /// <param name="selectCount">选取的元素的个数</param>
            static void Combination(List<string> lsArray, int selectCount)
            {
                int totolcount = lsArray.Count;
                int[] currectselect = new int[selectCount];
                int last = selectCount - 1;
                for (int i = 0; i < selectCount; i++)
                {
                    currectselect[i] = i;
                }
                while (true)
                {
                    for (int i = 0; i < selectCount; i++)
                    {
                        Console.Write(" {0} ", lsArray[currectselect[i]]);
                    }
                    Console.WriteLine();
                    if (currectselect[last] < totolcount - 1)
                    {
                        currectselect[last]++;
                    }
                    else
                    {
                        int pos = last;
                        while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)
                        {
                            pos--;
                        }
                        if (pos == 0) return;
                        currectselect[pos - 1]++;
                        for (int i = pos; i < selectCount; i++)
                        {
                            currectselect[i] = currectselect[i - 1] + 1;
                        }
                    }
                }
            }
    

     排列算法(递归):

            /// <summary>
            /// 对数组进行全排列
            /// </summary>
            /// <param name="lsArray">要进行全排列的数组</param>
            /// <param name="begin">进行全排列的开始下标</param>
            /// <param name="end">进行全排列的结束下标</param>
            static void array(List<string> lsArray, int begin, int end)
            {
                if (begin == end)
                {
                    for (int i = 0; i <= end; i++)
                        Console.Write(" {0} ", lsArray[i]);
                    Console.WriteLine();
                }
                for (int i = begin; i <= end; i++)
                {
                    Swap(lsArray, begin, i);
                    array(lsArray, begin + 1, end);
                    Swap(lsArray, begin, i);
                }
            }
    
            /// <summary>
            /// 交换数组中的下标为x,y的值
            /// </summary>
            /// <param name="lsArray">该数组</param>
            /// <param name="x"></param>
            /// <param name="y"></param>
            static void Swap(List<string> lsArray, int x, int y)
            {
                string t = lsArray[x];
                lsArray[x] = lsArray[y];
                lsArray[y] = t;
            }
    
  • 相关阅读:
    LibreOJ 6282 数列分块入门 6(在线插入在线查询)
    LibreOJ 6281 数列分块入门 5(分块区间开方区间求和)
    LibreOJ 6280 数列分块入门 4(分块区间加区间求和)
    LibreOJ 6279 数列分块入门 3(分块+排序)
    LibreOJ 6278 数列分块入门 2(分块)
    LibreOJ 6277 数列分块入门 1(分块)
    BZOJ 2301 Problem b(莫比乌斯反演+分块优化)
    MD5Untils加密工具类
    20160418javaweb之 Filter过滤器
    20160417javaweb之servlet监听器
  • 原文地址:https://www.cnblogs.com/rainnight/p/2557622.html
Copyright © 2020-2023  润新知