vba 二维数组的所有可能组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11326038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
All possible combinations of a 2D array
提问by Slartibartfast
I want to generate all possible combinations from a 2D [m x n] array except for the first element of each array. That element will stand for the 'type' signifying the rest elements. For example, if I've an array
我想从 2D [mxn] 数组生成所有可能的组合,除了每个数组的第一个元素。该元素将代表表示其余元素的“类型”。例如,如果我有一个数组
shirts[][] =
{
{"colour", "red", "blue", "green", "yellow"},
{"cloth", "cotton", "poly", "silk"},
{"type", "full", "half"}
};
The desired output should be combination of all the possibilities of shirt. For the above example,
所需的输出应该是衬衫的所有可能性的组合。对于上面的例子,
colour red
colour blue
...
cloth silk
type full
type half
colour red cloth cotton
colour red cloth poly
...
colour yellow type half
cloth cotton type full
...
cloth silk type half
colour red cloth cotton type full
...
colour yellow cloth silk type half
I tried something like this (also took help from other Stack Overflow Question)
我试过这样的事情(也从其他堆栈溢出问题中得到了帮助)
String shirts[][] =
{
{"colour", "red", "blue", "green", "yellow"},
{"cloth", "cotton", "poly", "silk"},
{"type", "full", "half"}
};
majorCombinations = new int[possibilities][shirts.length];
int currentCombination;
int offset = 1;
for (int i=0; i < shirts.length; i++)
{
currentCombination = 0;
while (currentCombination < possibilities)
{
for (int j=0; j < shirts[i].length; j++)
{
for (int k=0; k < offset; k++)
{
if (currentCombination < possibilities)
{
majorCombinations[currentCombination][i] = shirts[i][j];
currentCombination++;
}
}
}
}
offset *= shirts[i].length;
}
but it gives values of ALL n combinations only i.e.
但它只给出所有 n 个组合的值,即
colour cloth type
colour cloth full
...
yellow silk half
It doesn't take into account smaller combinations and it ain't even generic i.e. for an [m x n] array (n need not be fixed). A help in VBA would be highly appreciated. I'm comfortable with C, Java and C#. Thanks in advance :)
它没有考虑更小的组合,它甚至不是通用的,即对于 [mxn] 数组(n 不需要固定)。非常感谢 VBA 中的帮助。我对 C、Java 和 C# 很满意。提前致谢 :)
Edit:
编辑:
This is different than the question asked here. This one is not a Cartesian Product wherein oneelement be taken from each array in question. The output I require doesn't put this restriction; hence the number of combinations in this scenario > number of combinations in the linked question. Also, the first column is a descriptor of the contents and must accompany the content.
这与这里提出的问题不同。这不是笛卡尔积,其中从每个有问题的数组中取出一个元素。我需要的输出没有这个限制;因此,此场景中的组合数 > 链接问题中的组合数。此外,第一列是内容的描述符,必须伴随内容。
回答by Ebad Masood
For two arrays two nested loops should do:
对于两个数组,两个嵌套循环应该做:
for (int i = 0 ; i != c[0].length ; i++) {
for (int j = 0 ; j != c[1].length ; j++) {
System.out.writeln(""+c[0][i]+c[1][j]);
}
}
For more nesting you would need a recursive or an equivalent stack-based solution.
对于更多嵌套,您需要递归或等效的基于堆栈的解决方案。
void combos(int pos, char[][] c, String soFar) {
if (pos == c.length) {
System.out.writeln(soFar);
return;
}
for (int i = 0 ; i != c[pos].length ; i++) {
combos(pos+1, c, soFar + c[pos][i]);
}
}
回答by Dave Bish
Is what you want, a Cartesian product?
你想要的是笛卡尔积吗?
var colours = new[]{"colour - red", "colour - blue", "colour - green", "colour - yellow"};
var cloth = new[] {"cloth - cotton", "cloth - poly", "cloth - silk"};
var type = new[]{"type - full", "type - half"};
var combinations = from c in colours
from cl in cloth
from t in type
select new[]{c, cl, t};
回答by user1437203
for (int i = 0; i < shirts[0].length; i++) {
for (int j = 0; j < shirts[1].length; j++) {
for (int k = 0; k < shirts[2].length; k++) {
if (i != 0)
System.out.print(shirts[0][0] + " " + shirts[0][i]
+ " ");
if (j != 0)
System.out.print(shirts[1][0] + " " + shirts[1][j]
+ " ");
if (k != 0)
System.out.print(shirts[2][0] + " " + shirts[2][k]
+ " ");
System.out.println();
}
}
}
}