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

对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();

        }

    }

}

结果如下:

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


本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/11/17/list-CombinationPermutation.html,如需转载请自行联系原作者

上一篇:find命令与locate命令的区别


下一篇:Windows Server 2008自定义任务栏和开始菜单