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

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
爱范儿
爱范儿
Recent Announcements
Recent Announcements
AI
AI
V
Visual Studio Blog
H
Heimdal Security Blog
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
人人都是产品经理
人人都是产品经理
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
S
Secure Thoughts
S
Security Affairs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
Schneier on Security
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
The Cloudflare Blog
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
NISL@THU
NISL@THU
C
Cybersecurity and Infrastructure Security Agency CISA
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
罗磊的独立博客
V
Vulnerabilities – Threatpost
S
Securelist
N
News and Events Feed by Topic
Cloudbric
Cloudbric
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V2EX - 技术
V2EX - 技术
小众软件
小众软件

博客园 - 遗忘海岸

简协运动模拟 电磁场中的运动轨迹模拟 圆周运动模拟 带阻力的平抛运动 C# 队列的一些并发模拟 devexpress gridview master,detail视图 focuseRowHandle 同步选中 C# Tcp Server端实现,使用TcpListener 深信服务超融合管理Api调用 GDI+画工作流图的一些总结 绘制贝塞而曲线 GDI+画直线带箭头 devexpress schedulerControl Gantt View 使用 逻辑回归损失函数求导 matplotlim柱状图 匀加速运动模拟python,(matplotlib) python 类鸟群Boids C#动态编译 正太分布数据排序后分段数据的方差与标准差 Android 的一个通用列表单选AlertDialog封装
C# 实现排列
遗忘海岸 · 2023-09-26 · via 博客园 - 遗忘海岸

class Program
    {
        static int count = 0;
        static void Main(string[] args)
        {


            var remainList = new List<string>() { "A", "B", "C" , "D", "E","F","G" };//
            var selectedList = new List<String>();
            int m = 3;
             C(remainList, selectedList, m);

            Console.WriteLine("Count:{0}", count);
            Console.ReadLine();
        }

        public static void Disp(List<string> arr)
        {
            foreach (var it in arr)
            {
                Console.Write(it);
            }
            Console.WriteLine();
        }
        public static void C(List<String> remainList, List<String> selectedList, int m)
        {
     

            foreach (var item in remainList)
            {

                var subSelectedList = new List<String>();
                subSelectedList.AddRange(selectedList);

                subSelectedList.Add(item);
                #region 复制剩余的原始
                var remainList2 = new List<String>();
                foreach (var it in remainList)
                {
                    if (it != item)
                    {
                        remainList2.Add(it);
                    }
                }
                #endregion
                if (subSelectedList.Count < m)
                {
                     C(remainList2, subSelectedList, m );
                   
                }
                else
                {
                   count++;
                   Disp(subSelectedList);
                }

                
            }

           
        }
    }

View Code

生成组合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication15
{
    class Program
    {
        static int count = 0;
        static int count_g = 0;
        static void Main(string[] args)
        {
            var sw=new Stopwatch();
            sw.Start();
            for (int i = 0; i < 1; i++)
            {
                var arr = new List<String> { "A", "B", "C", "D", "E", "F", "G", "H" };//
                // var arr = new List<String> { "A", "B", "C"};//
                var selectedList = new List<String>();
                int m = 4;
               // C2(arr, selectedList, m, 0);
                C3(arr, selectedList, m);
                Console.WriteLine("Count:{0},Count_G:{1}", count, count_g);
            }
            sw.Stop();
            Console.WriteLine("duration:{0}", sw.ElapsedMilliseconds);
            Console.ReadLine();
        }

        public static void Disp(List<string> arr)
        {
            foreach (var it in arr)
            {
                Console.Write(it);
            }
            Console.WriteLine();
        }
        public static void C(List<String> remainList, List<String> selectedList, int m)
        {


            foreach (var item in remainList)
            {

                var subSelectedList = new List<String>();
                subSelectedList.AddRange(selectedList);

                subSelectedList.Add(item);
                #region 复制剩余的原始
                var remainList2 = new List<String>();
                foreach (var it in remainList)
                {
                    if (it != item)
                    {
                        remainList2.Add(it);
                    }
                }
                #endregion
                if (subSelectedList.Count < m)
                {
                    C(remainList2, subSelectedList, m);

                }
                else
                {
                    count++;
                    Disp(subSelectedList);
                }


            }


        }
        public static void C2(List<String> arr, List<String> selectedList, int m, int start)
        {

            var end = arr.Count ;

            for (int i = start; i < end; i++)
            {
                var item = arr[i];
                var subSelectedList = new List<String>();
                subSelectedList.AddRange(selectedList);

                subSelectedList.Add(item);
                #region 复制剩余的原始
                start++;
                #endregion
                if (subSelectedList.Count < m)
                {
                    C2(arr, subSelectedList, m,  start );

                }
                else
                {
                    count++;
                    Disp(subSelectedList);
                }


            }


        }
        public static void C3(List<String> remainList, List<String> selectedList, int m)
        {
            count_g++;

            for (int i = 0; i < remainList.Count; i++)
            {
                var item = remainList[i];

                var subSelectedList = new List<String>();
                subSelectedList.AddRange(selectedList);

                subSelectedList.Add(item);


                var len = subSelectedList.Count + (remainList.Count - (i + 1));
                if (len < m) return;
                #region 复制剩余的原始
                var remainList2 = new List<String>();
                for (int j = i + 1; j < remainList.Count; j++)
                {
                    var it = remainList[j];

                    remainList2.Add(it);

                }
                #endregion
                if (subSelectedList.Count < m)
                {


                    C3(remainList2, subSelectedList, m);


                }
                else
                {
                    count++;
                    Disp(subSelectedList);
                }


            }


        }
    }
}

View Code

public static void C2(List<String> arr, List<String> selectedList, int m, int start)
        {

            var end = arr.Count ;

            for (int i = start; i < end; i++)
            {
                var item = arr[i];
                var subSelectedList = new List<String>();
                subSelectedList.AddRange(selectedList);

                subSelectedList.Add(item);



                var len = subSelectedList.Count + (arr.Count - (i + 1));
                if (len < m) return;

                if (subSelectedList.Count < m)
                {
                    C2(arr, subSelectedList, m,  i+1 );

                }
                else
                {
                    count++;
                    Disp(subSelectedList);
                }


            }


        }

View Code