日期:2014-05-18 浏览次数:20957 次
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication2 { class Program { public static void Main() { string[] list = new string[] { "a", "b", "c", "d", "e", "MM" }; var X = 3; var cnt = 0; foreach (var s in 你的组合(list, X)) Console.WriteLine("第{0}个结果:\t{1}", ++cnt, string.Join(string.Empty, s)); Console.ReadKey(); } private static IEnumerable<string[]> 你的组合(string[] list, int X) { if (X == 1) { foreach (var s in list) yield return new string[] { s }; } else if (X > 1) { for (var i = 0; i < list.Length; i++) { var newList = list.Where((s, p) => p != i).ToArray(); foreach (var sub in 你的组合(newList, X - 1)) for (var j = 0; j < sub.Length; j++) { var ret = sub.ToList(); ret.Insert(j, list[i]); yield return ret.ToArray(); } } } } } }
------解决方案--------------------
要注意,输出当然有bad这种结果啦,不是一定按照原来排列顺序的。