数组的交叉组合算法,求帮助!
有数组:
string[] a = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k" };
要让它最后的组合成像:
a b;a c;a d;...
a b a;a b c;a b d;...
a c a;a c b;a c d;...
让数组内的每一个都相互组合,从两个一组开始,到数组长度一组。然后输出。
求高人帮忙啊!
------最佳解决方案-------------------- static void Main(string[] args)
{
List<string> list = new List<string>() { "a", "b", "c" };
bind(list, new List<string>(), 1);
Console.ReadLine();
}
private static void bind(List<string> list, List<string> source, int count)
{
if (source.Count <= count)
{
if (source.Count > 1)
Write(source);
}
for (int i = 0; i < list.Count; i++)
{
if (source.Contains(list[i].Trim()))
continue;
source.Add(list[i]);
bind(list, source, count + 1);
source.Remove(list[i]);
}
}
private static void Write(List<string> List)
{
for (int i = 0; i < List.Count; i++)
{
Console.Write(List[i]);
}
Console.Write("\n");
}
------其他解决方案--------------------