首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

字符组数的不反复组合方法

2012-12-22 
字符组数的不重复组合方法字符组数的不重复组合方法namespace KeyWordSplit{class Program{private static

字符组数的不重复组合方法
字符组数的不重复组合方法

namespace KeyWordSplit{    class Program    {        private static string[] KeyWord { get; set; }        public static void Main(string[] args)        {            KeyWord = new string[] { "我", "是", "你", "老", "板" };            foreach (string str in GetKeyWordCollection())            {                Console.WriteLine(str);            }            Console.ReadLine();        }        private static List<string> GetKeyWordCollection()        {            List<String> result = new List<string>();            string index = CharArrayToString(new char[KeyWord.Length]);            while (true)            {                List<string> tmp = new List<string>();                index = CalculationNextKeyWord(index);                if (Convert.ToInt32(index) == 0) break;                char[] ch = index.ToString().ToCharArray();                for (int i = 0; i < ch.Length; i++)                {                    if (!tmp.Contains(KeyWord[Convert.ToInt32(ch[i].ToString())]))                        tmp.Add(KeyWord[Convert.ToInt32(ch[i].ToString())]);                    else                    {                        tmp = null;                        break;                    }                }                if (tmp != null)                    result.Add(ListToString(tmp));            }            return result;        }        private static string CalculationNextKeyWord(string keyWordIndex)        {            char[] keyWordArr = keyWordIndex.ToCharArray();            int index = keyWordArr.Length;            while (index-- > 0)            {                int num = Convert.ToInt32(keyWordArr[index].ToString());                if (num + 1 >= keyWordArr.Length)                {                    keyWordArr[index] = '0';                    continue;                }                else                {                    int current = Convert.ToInt32(keyWordArr[index].ToString());                    keyWordArr[index] = (current + 1).ToString().ToCharArray()[0];                    break;                }            }            return CharArrayToString(keyWordArr);        }        private static string ListToString(List<string> array)        {            StringBuilder sb = new StringBuilder();            foreach (string str in array)            {                sb.Append(str);            }            return sb.ToString();        }        private static string CharArrayToString(char[] array)        {            StringBuilder sb = new StringBuilder();            foreach (char ch in array)            {                if (ch == 0)                    sb.Append("0");                else                    sb.Append(ch.ToString());            }            return sb.ToString();        }    }}

热点排行