Permutation

public class Combination
    {
        List<string> Array = null;
        public Combination(List<string> array)
        {
            Array = array;
        }
        public void PrintCombination()
        {
            foreach (string s in Combinate(0))
            {
                Console.WriteLine(s);
            }
        }
        private  List<string> Combinate(int startIndex)
        {
            if (startIndex >= Array.Count)
                return new List<string>();
            if (startIndex == Array.Count - 1)
                return new List<string>() { Array[startIndex] };
            else
            {
                List<string> temp = Combinate(startIndex + 1);
                List<string> result = new List<string>();
                result.Add(Array[startIndex]);
                foreach (string s in temp)
                {
                    result.Add(s);
                }
                foreach (string s in temp)
                {
                    result.Add(s + Array[startIndex]);
                }
                return result;
            }
        }

    }
    public class Permutation
    {
        List<string> Array = null;
        public Permutation(List<String> array)
        {
            Array = array;
        }
        public void PermutationShow(int startIndex)
        {
            if (startIndex >= Array.Count)
                return;
            if (startIndex == Array.Count - 1)
            {
                foreach (string s in Array)
                {
                    Console.Write(s);
                }
                Console.WriteLine();
            }
            else
            {
                for (int i = startIndex; i < Array.Count; i++)
                {
                    string temp = Array[i];
                    Array[i] = Array[startIndex];
                    Array[startIndex] = temp;

                    PermutationShow(startIndex + 1);

                    Array[startIndex] = Array[i];
                    Array[i] = temp;
                }
            }
        }
    }
posted @ 2012-02-22 16:20  xpwilson  阅读(235)  评论(0编辑  收藏  举报