码迷,mamicode.com
首页 > 其他好文 > 详细

c# 排列组合算法

时间:2014-07-22 08:01:36      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   for   re   

   //排列组合
    public class FullArrange {
        /// <summary>
        /// 排列组合
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="splitStr">分割的符号,比如";"</param>
        /// <returns></returns>
        public static List<string> GetArrangeResult(string str,string splitStr) {
            str = str.Trim();
            if (string.IsNullOrEmpty(str))
            {
                return new List<string>();
            }
            else if (str.Length == 1)
            {
                return new List<string>{ str};
            }
            else if (str.Length == 2)
            {
                string[] ca = str.Split(new char[] {Convert.ToChar(splitStr)});
                return new List<string>() { ca[0].ToString() + splitStr + ca[1].ToString(), ca[1].ToString() + splitStr + ca[0].ToString() };
            }
            else
            {
                string[] array = str.Split(new char[] { Convert.ToChar(splitStr)});
                List<string> Splittemp=new List<string>();
                List<string> temp = GetArrangeString(array[0].ToString(), array[1].ToString());
                for (int i = 2; i < array.Length; i++)
                {
                    int count = temp.Count;
                    for (int j = 0; j < count; j++)
                    {
                        temp.AddRange(GetArrangeString(temp[j], array[i]));
                        temp.Remove(temp[j]);
                        j--;
                        count--;
                    }
                }
                foreach (var item in temp)
                {
                    var  mstr="";
                    foreach (var s in item) {
                        mstr += s.ToString()+ ";";
                    }
                   
                    Splittemp.Add(mstr.TrimEnd(;));
                }

                return Splittemp;
            }
        }

        public static List<string> GetArrangeString(string parent, string child)
        {
            List<string> temp = new List<string>();

            for (int i = 0; i <= parent.Length; i++)
            {
                    temp.Add(parent.Insert(i, child.ToString()));
            }
            return temp;
        }
    }

c# 排列组合算法,布布扣,bubuko.com

c# 排列组合算法

标签:style   blog   color   os   for   re   

原文地址:http://www.cnblogs.com/chaohome/p/3857590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!