1. 程式人生 > >C# 排列組合取值

C# 排列組合取值

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

 

 

    class Program
    {
        static void Main(string[] args)
        {
            //List<string> list = new List<string>();
            //for (int i = 0; i < 6; i++)
            //{
            //    list.Add(i.ToString());
            //}

            List<string> list = new List<string> { "0", "1", "2", "5", "8", "4", "12", "32" }; 

            Combination.C(list, 3);
            Console.WriteLine("---------------------");
            //Console.Read();

            Combination.A(list, 0, 3);
            Console.WriteLine("---------------------");
            Console.Read();

        }
    }

 

   public abstract class Combination
    {
        public static List<string> GetCombination(List<string> SampleList, int m)
        {
            if (m == 1)
            {
                return SampleList;
            }
            List<string> result = new List<string>();
            if (SampleList.Count == m)
            {
                StringBuilder temp_sb = new StringBuilder();
                foreach (string s in SampleList)
                {
                    temp_sb.Append(s);
                }
                result.Add(temp_sb.ToString());
                Console.WriteLine(temp_sb.ToString());
                return result;
            }
            string temp_firstelement = SampleList[0];
            SampleList.RemoveAt(0);
            List<string> temp_samplist1 = new List<string>();
            temp_samplist1.AddRange(SampleList);
            List<string> temp_list1 = GetCombination(temp_samplist1, m - 1);
            foreach (string s in temp_list1)
            {
                result.Add(temp_firstelement + s);
                Console.WriteLine(temp_firstelement + s);
            }        
            List<string> temp_samplist2 = new List<string>();
            temp_samplist2.AddRange(SampleList);
            List<string> temp_list2 = GetCombination(temp_samplist2, m);
            result.AddRange(temp_list2);            
            return result;
        }

        /// <summary>
        /// 對陣列進行組合操作,選取selectCount個元素進行組合
        /// </summary>
        /// <param name="lsArray">即將進行組合操作的陣列</param>
        /// <param name="selectCount">選取的元素的個數</param>
        public static void C(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>
       public static void A(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);
                A(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;
        }
    }