我不知道怎么问我的问题在简洁的方式,所以我会用实例开始,并从那里展开。我与VBA的工作,但我觉得这个问题是不特定语言,并会只需要一个聪慧的头脑,可以提供一个伪code框架。在此先感谢您的帮助!
I'm not sure how to ask my question in a succinct way, so I'll start with examples and expand from there. I am working with VBA, but I think this problem is non language specific and would only require a bright mind that can provide a pseudo code framework. Thanks in advance for the help!
例如:我有3个字符数组,像这样:
Example: I have 3 Character Arrays Like So:
Arr_1 = [X,Y,Z] Arr_2 = [A,B] Arr_3 = [1,2,3,4]我想生成字符数组的所有可能的排列,像这样:
I would like to generate ALL possible permutations of the character arrays like so:
XA1 XA2 XA3 XA4 XB1 XB2 XB3 XB4 YA1 YA2 . . . ZB3 ZB4这可以用3迎刃而解while循环或循环。我的问题是我该如何解决这个,如果阵列的#是未知的,每个数组的长度未知?
This can be easily solved using 3 while loops or for loops. My question is how do I solve for this if the # of arrays is unknown and the length of each array is unknown?
,以便与4字符数组的例子:
So as an example with 4 character arrays:
Arr_1 = [X,Y,Z] Arr_2 = [A,B] Arr_3 = [1,2,3,4] Arr_4 = [a,b]我需要生成:
XA1a XA1b XA2a XA2b XA3a XA3b XA4a XA4b . . . ZB4a ZB4b因此,广义的例子是:
So the Generalized Example would be:
Arr_1 = [...] Arr_2 = [...] Arr_3 = [...] . . . Arr_x = [...]有没有办法来组织,这将产生一个未知号码循环和循环通过每个数组的长度,生成排列的函数?或者,也许有思考问题的更好的办法?
Is there a way to structure a function that will generate an unknown number of loops and loop through the length of each array to generate the permutations? Or maybe there's a better way to think about the problem?
谢谢大家!
推荐答案这实际上是最简单,最简单的解决方案。以下是在Java中,但它应该是有启发性:
Recursive solution
This is actually the easiest, most straightforward solution. The following is in Java, but it should be instructive:
public class Main { public static void main(String[] args) { Object[][] arrs = { { "X", "Y", "Z" }, { "A", "B" }, { "1", "2" }, }; recurse("", arrs, 0); } static void recurse (String s, Object[][] arrs, int k) { if (k == arrs.length) { System.out.println(s); } else { for (Object o : arrs[k]) { recurse(s + o, arrs, k + 1); } } } }(查看完整的输出)
注:Java数组是基础,所以 K 从云 0..arrs.length-1 递归,直到在 K == arrs.length 时,它的递归的终点。
Note: Java arrays are 0-based, so k goes from 0..arrs.length-1 during the recursion, until k == arrs.length when it's the end of recursion.
它也可以写一个非递归的解决方案,但坦率地说,这是不太直观。这实际上是非常相似的基转换,例如从十进制为十六进制;这是一个广义的形式,其中每个位置有自己的一套价值观。
It's also possible to write a non-recursive solution, but frankly this is less intuitive. This is actually very similar to base conversion, e.g. from decimal to hexadecimal; it's a generalized form where each position have their own set of values.
public class Main { public static void main(String[] args) { Object[][] arrs = { { "X", "Y", "Z" }, { "A", "B" }, { "1", "2" }, }; int N = 1; for (Object[] arr : arrs) { N = N * arr.length; } for (int v = 0; v < N; v++) { System.out.println(decode(arrs, v)); } } static String decode(Object[][] arrs, int v) { String s = ""; for (Object[] arr : arrs) { int M = arr.length; s = s + arr[v % M]; v = v / M; } return s; } }(查看完整的输出)
这会产生不同的顺序的tuplets。如果你想生成他们在相同的顺序递归的解决方案,那么你通过 ARRS 落后在德code 如下:
This produces the tuplets in a different order. If you want to generate them in the same order as the recursive solution, then you iterate through arrs "backward" during decode as follows:
static String decode(Object[][] arrs, int v) { String s = ""; for (int i = arrs.length - 1; i >= 0; i--) { int Ni = arrs[i].length; s = arrs[i][v % Ni] + s; v = v / Ni; } return s; }(查看完整的输出)
更多推荐
生成字符组合的所有排列时,每个数组的数组和长度#不明
发布评论