1

我有一个清单

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

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

我想要的是:

对于给定的权重,得到所有可能的组合:

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

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

我很难生成它。请指导?

4

3 回答 3

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

于 2012-04-11T10:33:08.313 回答
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); 
                }
            }
    }

在此处输入图像描述

用法:

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

输出:

a0b0
a0b1
a0b2
a1b0
a1b1
a1b2
a2b0
a2b1
a2b2
于 2012-04-11T12:38:08.530 回答
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-CG

例如:

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);
}
于 2012-04-11T10:56:14.147 回答