简体   繁体   中英

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.

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.

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.

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!

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

  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. The maximum value of this loop is 10 ^ 4 or 10,000. The number of unique characters in the equation determines the powers of 10 you'll use. The maximum maximum is 10 ^ 10 which is bigger than an int . A long would work.

  3. Inside the iteration, split the index into N digits. In your example, that would be 4 digits. Since we're iterating from 1,000 to 9,999 inclusive (10,000 excluded) all of the index values will have 4 digits.

  4. Check to make sure the N digits are unique. 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> .

  6. Output the List values or no solution found after completing the iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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