二维数组的所有可能组合

编程入门 行业动态 更新时间:2024-10-27 08:31:01
本文介绍了二维数组的所有可能组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从 2D [m x n] 数组生成所有可能的组合,每个数组的第一个元素除外.该元素将代表表示其余元素的类型".例如,如果我有一个数组

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

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

我尝试过这样的事情(也从其他 Stack Overflow 获得帮助 问题 )

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; }

但它只给出所有 n 个组合的值,即

but it gives values of ALL n combinations only i.e.

colour cloth type colour cloth full ... yellow silk half

它没有考虑更小的组合,它甚至不是通用的,即对于 [m x n] 数组(n 不需要固定).对 VBA 的帮助将不胜感激.我对 C、Java 和 C# 很满意.提前致谢:)

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 :)

这与此处提出的问题不同.这个不是笛卡尔积,其中 一个 元素从每个有问题的数组中取出.我需要的输出没有这个限制;因此,这种情况下的组合数 > 链接问题中的组合数.另外,第一列是内容的描述符,必须伴随内容.

This is different than the question asked here. This one is not a Cartesian Product wherein one element 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.

推荐答案

原始答案链接

对于两个数组,两个嵌套循环应该做:

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]); } }

更多推荐

二维数组的所有可能组合

本文发布于:2023-11-30 00:26:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!