博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

对List内元素子集合进行组合排列

Posted on 2009-11-17 21:08  生鱼片  阅读(999)  评论(0编辑  收藏  举报

List中元素子集合进行组合排列,使用了递归,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {                        

            CombinationsPermutations("asdf", 3);        

        }

 

        private static void RotateLeft<T>(IList<T> sequence, int start, int count)

        {

            T tmp = sequence[start];

            sequence.RemoveAt(start);

            sequence.Insert(start + count - 1, tmp);

        }

 

        public static IEnumerable<IList<T>> Combinations<T>(

            IList<T> sequence,

            int start,

            int count,

            int choose)

        {

            if (choose == 0) yield return sequence;

            else

            {

                for (int i = 0; i < count; i++)

                {

                    foreach (var perm in Combinations(

                                                sequence,

                                                start + 1,

                                                count - 1 - i,

                                                choose - 1))

                        yield return perm;

                    RotateLeft(sequence, start, count);

                }

            }

        }

 

        public static IEnumerable<IList<T>> Combinations<T>(IList<T> sequence,int choose)

        {

            return Combinations(sequence, 0, sequence.Count, choose);

        }

        public static IEnumerable<IList<T>> Permutations<T>(IList<T> sequence, int count)

        {

            if (count == 1) yield return sequence;

            else

            {

                for (int i = 0; i < count; i++)

                {

                    foreach (var perm in Permutations(sequence, count - 1))

                        yield return perm;

                    RotateRight(sequence, count);

                }

            }

        }

 

        public static IEnumerable<IList<T>> Permutations<T>(IList<T> sequence)

        {

            return Permutations(sequence, sequence.Count);

        }

        public static void RotateRight<T>(IList<T> sequence, int count)

        {

            T tmp = sequence[count - 1];

            sequence.RemoveAt(count - 1);

            sequence.Insert(0, tmp);

        }     

 

        private static void CombinationsPermutations(string s, int count)

        {

            foreach (var combo in Combinations(s.ToCharArray().ToList(),count))

            {

                foreach (var permu in Permutations(combo, count))

                {

                    string r = new string(permu.Take(count).ToArray());

                    Console.Write("{0,-8}", r);

                }

            }

            Console.WriteLine();

        }

    }

}

结果如下:

clip_image002