惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - 飘渺峰

一致性Hash算法 .net Parallel并行使用注意事项 析构函数和Dispose方法的区别 查看SQLServer的最大连接数 Hash算法-CityHash算法 Hash算法 Sunday算法--C#版 KMP算法--C#版 BoyerMoore(BM)算法--C# HostFileChangeMonitor [转]软件项目管理总体流程设计 生活 负载均衡算法--C#版 结束进程的方法forceStopPackage 【转】OAUTH协议简介 SQL Server FOR XML PATH 语句的应用 nlog轻量级日志组件 反射加载程序集的几个方法的区别 windows 下TCP最大连接数
全排列和组合算法
飘渺峰 · 2012-06-21 · via 博客园 - 飘渺峰

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;
        }