日期:2014-05-20 浏览次数:20942 次
static string[] m_Data = { "A", "B", "C", "D", "E" }; static void Main(string[] args) { Dictionary<string, int> dic = new Dictionary<string, int>(); for (int i = 0; i < m_Data.Length; i++) { Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉 dic.Add(m_Data[i], i); } GetString(dic); Console.ReadLine(); } static void GetString(Dictionary<string,int> dd) { Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (KeyValuePair<string, int> kv in dd) { for (int i = kv.Value + 1; i < m_Data.Length; i++) { Console.WriteLine(kv.Key + m_Data[i]); dic.Add(kv.Key + m_Data[i], i); } } if(dic.Count>0) GetString(dic); }
static void Main(string[] args) { string[] arr = new[] { "A", "B", "C", "D", "E" }; GetCombination(arr); } static void GetCombination(string[] nums) { double count = Math.Pow(2, nums.Length); for (int i = 1; i <= count - 1; i++) { string str = Convert.ToString(i, 2).PadLeft(nums.Length, '0'); for (int j = 0; j < str.Length; j++) { if (str[j] == '1') { Console.Write(nums[j]); } } Console.WriteLine(); } } /* 输出: E D DE C CE CD CDE B BE BD BDE BC BCE BCD BCDE A AE AD ADE AC ACE ACD ACDE AB ABE ABD ABDE ABC ABCE ABCD ABCDE */
------解决方案--------------------
学习!!!
------解决方案--------------------
楼主写的不错,就是没有诚心与网友们分享。
试问没有注释的代码怎么分享?
------解决方案--------------------
------解决方案--------------------
学习!
------解决方案--------------------
厉害
------解决方案--------------------
刚好对我有用,谢了
------解决方案--------------------
up