简体   繁体   English

使用 Java 中的排列来求解方程

[英]Using Permutations in Java for solving an equation

For a given problem I am granted with two numbers that will sum to a certain result in this way:对于给定的问题,我被授予两个数字,它们将以这种方式求和为某个结果:

num1 + num2 = result

However these numbers will be "coded", so, for example, an input may be:然而,这些数字将被“编码”,因此,例如,输入可能是:

ab + bc = acd

The numbers can have any length up to 9 digits.数字可以有任意长度,最多 9 位。

My task is to create an efficient method to find how many possible solutions each input has or if it is impossible.我的任务是创建一种有效的方法来查找每个输入有多少可能的解决方案,或者是否不可能。

I have tackled this issue by creating a class called Number (in java), that has a letter (char type) and a numeric value (int) and then I converted each number to an array of this new class Number, so I can easily check their current values.我通过创建一个名为 Number(在 Java 中)的 class 解决了这个问题,它有一个字母(char 类型)和一个数值(int),然后我将每个数字转换为这个新的 class 数字的数组,所以我可以很容易地检查它们的当前值。

However I'm not so sure how to approach this backtracking issue.但是我不太确定如何处理这个回溯问题。

I am aware that I should start comparing the units from number1 and number2, then the tens, and so on, in each "level" selecting only the cases in which the sum will be logical, so for example in the above example for the units b=2 and c= 3 it would only make sense if d=5, and I could discard all the other cases in which d.= 5 for those two preexisting values.我知道我应该开始比较 number1 和 number2 的单位,然后是十位,依此类推,在每个“级别”中只选择总和合乎逻辑的情况,例如在上面的单位示例中b=2 和 c= 3 只有当 d=5 时才有意义,对于这两个预先存在的值,我可以丢弃 d.= 5 的所有其他情况。

I am told it is possible to cover all these permutations using only two cycles: One for cyclcing through the possible numbers [0 to 9] and one to cover all the letters/variables, but I don't quite understand how it could be done!有人告诉我可以只使用两个循环来覆盖所有这些排列:一个用于循环可能的数字 [0 到 9],一个用于覆盖所有字母/变量,但我不太明白如何完成!

If I had to imagine it I guess I could think of it as如果我必须想象它,我想我可以把它想象成

for cycle ( through "columns" from the rightmost up to the leftmost column of the shortest number)
  for cycle( for numbers 0 to 9?) 

but still I can't wrap my head around this approach to the subject, or how I could tackle this with recursion instead, all help to improve my understanding is very much appreciated.但是我仍然无法围绕这个主题的这种方法,或者我如何用递归来解决这个问题,非常感谢所有有助于提高我的理解的方法。

Here's one algorithm that should work.这是一种应该有效的算法。

Let's take your example equation:让我们以您的示例等式为例:

ab + bc = acd
  1. Determine the number of unique characters in the equation.确定等式中唯一字符的数量。 In your example, that would be four (a, b, c, d).在您的示例中,这将是四个(a、b、c、d)。

  2. In order to iterate through all of the possibilities, we're going to set up a loop.为了遍历所有的可能性,我们将建立一个循环。 The minimum value of this loop is 10 ^ 3 or 1,000.此循环的最小值为 10 ^ 3 或 1,000。 The maximum value of this loop is 10 ^ 4 or 10,000.此循环的最大值为 10 ^ 4 或 10,000。 The number of unique characters in the equation determines the powers of 10 you'll use.等式中唯一字符的数量决定了您将使用的 10 的次方。 The maximum maximum is 10 ^ 10 which is bigger than an int .最大值为 10 ^ 10,大于int A long would work.一个long的工作。

  3. Inside the iteration, split the index into N digits.在迭代中,将索引拆分为 N 位数字。 In your example, that would be 4 digits.在您的示例中,这将是 4 位数字。 Since we're iterating from 1,000 to 9,999 inclusive (10,000 excluded) all of the index values will have 4 digits.由于我们从 1,000 迭代到 9,999(包括 10,000)(不包括 10,000),因此所有索引值都将具有 4 位数字。

  4. Check to make sure the N digits are unique.检查以确保 N 位数字是唯一的。 No duplicates.没有重复。 This will eliminate most of the index values at the start.这将在开始时消除大部分索引值。

  5. Substitute the digits into the equation and check to see if the equation is valid.将数字代入方程式并检查方程式是否有效。 If so, save the equation in a List<String> .如果是这样,请将等式保存在List<String>中。

  6. Output the List values or no solution found after completing the iteration. Output List值或完成迭代后未找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM