得到數字全排組合

  string[] Sample = new string[] { "0", "1", "2", "3", "4"};
            List<string> SampleList = new List<string>();
            SampleList.AddRange(Sample);
            List<string> result = getCombination(SampleList, 2);


        private  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;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章