遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

C# 实现排列

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

 

posted on 2023-09-26 16:42  遗忘海岸  阅读(12)  评论(0编辑  收藏  举报