非递归生成元素的所有可能的排列从两个阵列递归、阵列、排列、元素

2023-09-11 07:19:41 作者:适可而止

我想生成特定运营商的String数组所有可能的方程式(+, - ,*,/)和一个变量字符串数组(A,B,C ...)。每个方程将组成对的变量和数字(一个+ B- C / B),除了最后一个变量,它没有操作者跟随它。该算法必须产生的可变长度的等式(2项,第6项,等等)。什么是最有效的方式来生成该列表中的Java?

I'm trying to generate all possible equations given an String array of operators (+,-,*,/) and an String array of variables (a,b,c ...). Each equation will be composed of pairs of variables and numbers (a+ b- c/ b), except the last variable, which has no operator following it. The algorithm must generate equations of variable lengths (2 terms, 6 terms, etc). What would be the most efficient way to generate this list in Java?

请,不这样做递归。 :)

Please, don't do it recursively. :)

嗯,不,这不是功课。它是一个个人项目,我正在尝试利用遗传算法来找到最佳的方程拟合数据。笼统的算法的描述就足够了,如果你相信如此。

Um, no this is not homework. Its a personal project where I'm trying to utilize genetic algorithms to find optimal equations to fit data. A description of an algorithm in general terms would suffice if you believe so.

推荐答案

既然你说,它不做作业,我就是一个信任的家伙......

Since you say its not homework, and I'm a trusting fellow...

通过对运营阵列运行的每个变量建立可变运营商组合的一个新的数组=> [A +,A-,A *,A /,B +.. D /。我们将这个BuiltArray1

Build a new array of variable-operator combinations by running each variable against the operator array => ["a+", "a-", "a*", "a/", "b+" ... "d/"]. We'll call this BuiltArray1

运行此阵攻击的经营者,允许输出每一个新的阵列还存储每个=> [A + A,A + B,A + C,A + D, AA......D / D。我们将这个BuiltArray2。现在,我们可以,如果我们关心删除原来的变量数组[一个,B,C,D]来 - 我们不会再使用它

Run this array against the operators, outputing each and also storing each in a new array => ["a+a", "a+b", "a+c", "a+d", "a-a" ..."d/d"]. We'll call this BuiltArray2. We can delete the original variable array ["a, "b", "c", "d"] now if we care to - we won't use it again.

现在事情变得更有趣......现在我们正在建设BuiltArray3。运行在BuiltArray1每个项目对中BuiltArray2每个项目,输出每个和在BuiltArray3存储每个=> [A + A + A,A-A + A,一个* A + A,一个/ A +一,B + A + A...D / D / D]。我们现在可以删除BuiltArray2节省一些内存(这将开始迅速消耗内存!)

Now things get more fun... now we are building BuiltArray3. Run each item in BuiltArray1 against each item in BuiltArray2, outputting each and storing each in BuiltArray3 => ["a+a+a", "a-a+a", "a*a+a", "a/a+a", "b+a+a" ... "d/d/d"]. We can now delete BuiltArray2 to save some memory (this will start to consume memory quickly!)

有关BuiltArray4,直到我们的计算机模具尖叫的最后BuiltArray_n它可以处理,我们运行在BuiltArray1对previously内置阵列中的每个项目,输出也存储每个结果在一个新的数组,然后删除previous阵列。

For BuiltArray4 until our computer dies screaming on the last BuiltArray_n it can handle, we run each item in BuiltArray1 against the previously built array, outputting and also storing each result in a new array, then deleting the previous array.

这将嚼过的内存和处理能力,但我想不出什么更优雅了我的头顶部。

This will chew through memory and processing power, but I can't think of anything more graceful off of the top of my head.

希望它帮助。

下面是红宝石codeZ:

Here's Ruby codez:

@arr1 = ["a", "b", "c", "d"]
arr2 = ["+", "-", "*", "/"]
@base = []
@arr1.each do |char|
  arr2.each do |op|
    @base << char + op
  end
end
@right_vals = @arr1
loop do
  @new_values = []
  @base.each do |left|
    @right_vals.each do |right|
      val = left + right
      puts val
      @new_values << val
    end
  end
  @right_vals = @new_values
end