递归生成列表中每个项目的组合

时间:2012-04-11 10:27:53

标签: c#

我有一个列表

List<string> collection = {"a","b"}

我想给出一个数值,即重量,例如2

我想要的是:

对于给定的体重,获得所有可能的组合:

a0b0, a1b0, a2b0
a0b1, a1b1, a2b1
a0b2, a2b2

其中0,1,2是从0到给定权重值的值

我很难生成它。请指导?

3 个答案:

答案 0 :(得分:3)

如果您真的需要所有可能的组合,请使用:

List<string> collection = new List<string> {"a","b"};
List<int> numbers = new List<int> { 0, 1, 2 };
var result = 
from a in collection
from b in collection
from n1 in numbers
from n2 in numbers
select a + n1 + b + n2;

这导致36项: a0a0,a0a1,a0a2,a1a0, A1A1, A1A2, a2a0, a2a1, A2A2, A0B0, a0b1, a0b2, a1b0, A1B1, A1B2, A2B0, A2B1, A2B2, b0a0, b0a1, b0a2, B1A0, b1a1, b1a2, B2A0, B2A1, b2a2,b0b0,b0b1,b0b2,b1b0,b1b1,b1b2,b2b0,b2b1,b2b2

如果您只需要在问题中说明的组合,请使用:

List<int> numbers = new List<int> { 0, 1, 2 };
var result = 
from n1 in numbers
from n2 in numbers
select "a" + n1 + "b" + n2;

这导致只有9项: A0B0, a0b1, a0b2, a1b0, A1B1, A1B2, A2B0, A2B1, a2b2

答案 1 :(得分:1)

使用递归:

static void ShowCombination(List<string> mlist, int value,int current=0,string stringleft="")
    {
            if (current == mlist.Count-1) // if this is the last item in the list
            {
                for (int m = 0; m <= value; m++) //loop through the value add it to the existing string-stringleft
                {
                    Console.WriteLine(stringleft  + mlist[current]+m.ToString()); 
                }
            }
            else // if there are more than 1 item left in the list
            {
                string currentstring = mlist[current]; //get current string in the list eg. "a" 
                stringleft = stringleft + currentstring ; //reset existing string -- eg "a"
                for (int m = 0; m <= value; m++)  //loop through the value add it to the existing 'stringleft' pass it and the new current index for recursion
                {
                    string stopass = stringleft +  m.ToString(); // eg. "a0"; "a1" 
                    ShowCombination(mlist, value, current + 1, stopass); 
                }
            }
    }

enter image description here

用法:

ShowCombination(new List<string>() {"a", "b"}, 2);

输出:

a0b0
a0b1
a0b2
a1b0
a1b1
a1b2
a2b0
a2b1
a2b2

答案 2 :(得分:0)

这对@ Daniel的答案并没有多大帮助,只是你的重量动态的方式:

int weight = 2;
List<string> collection1 = new List<string>{ "a", "b" };
var collection2 = Enumerable.Range(0, weight + 1);
var combinations=from str1 in collection1 
                 from int1 in collection2 
                 from str2 in collection1 
                 from int2 in collection2 
                 select str1 + int1 + str2 + int2;

foreach (var combi in combinations)
    Console.WriteLine(combi);

编辑:如果您想要所有排列,请查看此项目以了解它是如何实现的。它工作得很好。

http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

例如:

List<string> collection1 = new List<string> { "a", "b", "c", "d" };
var collection2 = Enumerable.Range(0, weight + 1);
collection1 = collection1.Concat(collection2.Select(i => i.ToString())).ToList();

var permutations = new Facet.Combinatorics.Permutations<String>(collection1);
foreach (IList<String> p in permutations)
{
    String combi = String.Join("", p);
}