count = len(len-1)/n
arr={1,2,3}
len=3
n=2
组合的个数 count = len(len-1)/2 = 3(3-1)/2 =3
----------------------
得到的组合结果为
1,2
1,3
2,3
----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Testing
{
class Program
{
static void Main(string[] args)
{
TestDoubleComposition(); Console.ReadLine();
} static List<string> m_list;
/// <summary>
/// 两两组合算法
/// </summary>
static void TestDoubleComposition()
{
m_list = new List<string>();
List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
DoubleComposition(list);
m_list.ForEach(o => {
Console.WriteLine(o);
});
} /// <summary>
/// 两两组合算法
/// </summary>
static void DoubleComposition(List<string> arr)
{
if (arr == null || arr.Count <= 1)
return; string str = arr.First();
arr.RemoveAt(0);
arr.ForEach(o =>
{
m_list.Add(string.Format("{0},{1}",str,o));
}); DoubleComposition(arr);
}
}
}