List Combinations c#

a 夏天 提交于 2019-12-23 06:35:11

问题


I have a 4 collections.

{ A1, A2, A3, ...}
{ B1, B1, B3, ...}
{ C1, C2, C3, ...}
{ D1, D2, D3, ...}

I need to find all of the possible combinations using the following rules:

  1. 1 item from the first and fourth collections
  2. 2 items from the second collection
  3. 3 items from the third

Example combinations:

{A1, B1, B2, C1, C2, C3, D1}
{A2, B1, B2, C1, C2, C3, D1}

回答1:


Visual Studio was taking too long to load, so I did it in JavaScript since I could test in my console. This will print out all the choices. (Also it sounds more like a "what's the algorithm for this?" not "what's the algorithm for this in C#?")

function makeGroup(id, count) {
  var result = [];
  for (var i = 1; i <= count; i++) {
    result.push(id + i);
  }
  return result;
}

function choose(group, count) {
  if (count === 1) {
    var result = [];
    for (var i = 0; i < group.length; i++) {
      result.push([group[i]]);
    }
    return result;
  }
  if (count === 2) {
    var result = [];
    for (var i = 0; i < group.length; i++) {
      for (var j = 0; j < group.length; j++) {
        if (i !== j && 
            i < j) {
          result.push([group[i], group[j]]);
        }
      }
    }
    return result;
  }
  if (count === 3) {
    var result = [];
    for (var i = 0; i < group.length; i++) {
      for (var j = 0; j < group.length; j++) {
        for (var k = 0; k < group.length; k++) {
          if (i !== j && i !== k && j !== k &&
              i < j && j < k) {
            result.push([group[i], group[j], group[k]]);
          }
        }
      }
    }
    return result;
  }
}

var groupA = makeGroup('A', 2);
var groupB = makeGroup('B', 2);
var groupC = makeGroup('C', 3);
var groupD = makeGroup('D', 1);

choose(groupA, 1).forEach(function (a) { 
  choose(groupB, 2).forEach(function (b) {
    choose(groupC, 3).forEach(function (c) {
      choose(groupD, 1).forEach(function (d) {
        console.log(a + " " + b[0] + " " + b[1] + " " + c[0] + " " + c[1] + " " + c[2] + " " + d[0]);
      });
    });
  });
});

Example output:

A1 B1 B2 C1 C2 C3 D1
A2 B1 B2 C1 C2 C3 D1

To see an example of it working, you can do:

var testGroup = makeGroup('T', 4);

var choose1 = choose(testGroup, 1);
var choose2 = choose(testGroup, 2);
var choose3 = choose(testGroup, 3);

console.log(JSON.stringify(choose1));
console.log(JSON.stringify(choose2));
console.log(JSON.stringify(choose3));

And see that it chooses correctly:

[["T1"],["T2"],["T3"],["T4"]]
[["T1","T2"],["T1","T3"],["T1","T4"],["T2","T3"],["T2","T4"],["T3","T4"]]
[["T1","T2","T3"],["T1","T2","T4"],["T1","T3","T4"],["T2","T3","T4"]]



回答2:


I've created this code to do what you want:

var aa = new[] { "A1", "A2", "A3", "A4" };
var bb = new[] { "B1", "B2", "B3", "B4" };
var cc = new[] { "C1", "C2", "C3", "C4" };
var dd = new[] { "D1", "D2", "D3", "D4" };

var query =
    from a in aa.SelectMembers()
    from b1 in bb.SelectMembers()
    from b2 in b1.Remainder.SelectMembers()
    from c1 in cc.SelectMembers()
    from c2 in c1.Remainder.SelectMembers()
    from c3 in c2.Remainder.SelectMembers()
    from d in aa.SelectMembers()
    select new []
    {
        a.Selected, b1.Selected, b2.Selected, c1.Selected,
        c2.Selected, c3.Selected, d.Selected,
    };

It produces, for my example, 4,608 combinations.

A1, B1, B2, C1, C2, C3, A1 
A1, B1, B2, C1, C2, C3, A2 
A1, B1, B2, C1, C2, C3, A3 
...
A4, B4, B3, C4, C3, C2, A3 
A4, B4, B3, C4, C3, C2, A4 

The two supporting bits of code you need are:

public static class Ex
{
    public static IEnumerable<Member<T>> SelectMembers<T>(this IEnumerable<T> @this)
    {
        if (@this == null || !@this.Any())
        {
            yield break;
        }
        else
        {
            for (var i = 0; i < @this.Count(); i++)
            {
                yield return new Member<T>(@this.Skip(i).First(), @this.Take(i).Concat(@this.Skip(i + 1)));
            }
        }
    }
}

public sealed class Member<T>
{ 
    private readonly T _Selected; 
    private readonly IEnumerable<T> _Remainder; 

    public T Selected { get { return _Selected; } } 
    public IEnumerable<T> Remainder { get { return _Remainder; } } 

    public Member(T Selected, IEnumerable<T> Remainder) 
    { 
        _Selected = Selected; 
        _Remainder = Remainder;    
    } 
}



回答3:


        string[] A = { "a1", "a2", "a3", "a4", "a5" };
        string[] B = { "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10"};
        string[] C = { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15"};
        string[] D = { "d1", "d2", "d3", "d4", "d5"};
        for(int i=0; i<A.Length;i++)
        {
            Console.Write(A[i]);
            for (int j = i * 2; j < i*2 +2; j++)
            {
                Console.Write(B[j]);                    
            }
            for (int k = i * 3; k < i*3+3; k++)
            {
                Console.Write(C[k]);
            }
            Console.WriteLine(D[i]);
        }
        Console.Read();

Output will be like this:

a1b1b2c1c2c3d1 a2b3b4c4c5c6d2 .....



来源:https://stackoverflow.com/questions/33226792/list-combinations-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!