如何找到两个一组的列表的所有组合?

How to find all combinations of an List in sets of two?

我已经尝试寻找,但没有找到太多我想要完成的东西。

假设我有一个 List<int> 大约有 50 个数字

List<int> _myList = new List<int>();
for (int i = 0; i < 49; i++)
{
    _myList.Add(i);
}

如何获取基于两个数组的组合列表?

例如 我的结果集看起来像

  1. 1,1
  2. 1,2
  3. 1,3
  4. 1,4
  5. 1,5

那些被认为是独一无二的。可以说 1,22,1 相同吗?

var sets;

for (int i = 0; i < 49; i++)
{
   for (int j = 1; j < 49; j++)
   {
      if(setc.Contains(new Pair(_myList(i), _myList(j))==false)
      {
         sets.Add(new Pair(_myList(i), _myList(j))
      }
   }
}

我假设您的源列表名为 input:

var output = new List<HashSet<int>>();
for (int i = 0; i < input.Count; i++)
    for (int j = i + 1; j < input.Count; j++)
        output.Add(new HashSet<int> { input[i], input[j] });

如果您想将结果实际输出到控制台:

foreach (var result in output)
    Console.WriteLine(string.Join(", ", result));

如果您只需要总组合,那么有一个公式

totalCombination = n!/(k! * (n-k)!)

如果n=50k=2

我们可以解析为50!/2!*48!

但是要以编程方式解决它

for(int i=0;i<49;i++)
{
    for(int j=i+1;j<=49;j++)
    {
        //Collect all the combinations in the form of 'i, j'
    }
}