Implementation of Java full permutation algorithm---small example

Source: Internet
Author: User

Question: the equation of Guess

Look at the following equation:

--x--=--x---

It means: two double-digit multiplication equals a two-digit number multiplied by a three-digit number.

If there are no qualifying conditions, there are many examples.

However, the current limit is: The 9 blocks, representing the 1~9 9 numbers, does not contain 0.

Each number from 1 to 9 in the calculation appears and appears only once!

Like what:

x = x 158

x = 138 X

x = 186 X

.....

Please program, output all possible situations!

Attention:

The two multiplier on the left is the same scheme, do not repeat the output!

Output order of different scenarios is not important


Ideas:

See this question, immediately think of the use of brute force to solve the problem, that is: write a few layers for the loop to operate, but, too cumbersome, I think, there are no other ideas, Lenovo to the previous paragraph looked at the full permutation algorithm, and then, the use of this idea to encode, the source code is as follows:

/**
* @ Shishihu
*
*/
public class Question3 {
public static int total = 0; Record the number of formulas that match the criteria

Exchange two values
public static void Swap (int[] intarrs, int i, int j) {
int temp;
temp = Intarrs[i];
Intarrs[i] = Intarrs[j];
INTARRS[J] = temp;
}

Full permutation algorithm
public static void Arrange (int[] intarrs, int st, int len) {
if (st = = len-1) {
if (Intarrs[0] < intarrs[2]) {
if ((intarrs[0] * + intarrs[1]) * (intarrs[2] * + intarrs[3]) = = ((intarrs[4] * + intarrs[5]) * (intarrs[6] * 100 + intarrs[7] * + intarrs[8])) {
System.out.println (Intarrs[0] + "" + intarrs[1] + "*" + intarrs[2] + "+ intarrs[3" + "=" + intarrs[4] + "" + intarrs[5 ] + "*" + intarrs[6] + "" + intarrs[7] + "" + intarrs[8]);
total++;
}
}
Print all the full array
/*for (int i = 0; i < len; i++) {
System.out.print (Intarrs[i] + "");
}*/
} else {
for (int i = st; i < Len; i++) {
Swap (Intarrs, St, i);
Arrange (Intarrs, St + 1, Len);
Swap (Intarrs, St, i);
}
}
}
public static void Main (string[] args) {
int intarrs[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Arrange (intarrs, 0, intarrs.length);
SYSTEM.OUT.PRINTLN (total);
}
}

Printing results:

46 * 79 = 23 * 158
54 * 69 = 27 * 138
54 * 93 = 27 * 186
58 * 67 = 29 * 134
58 * 69 = 23 * 174
58 * 73 = 29 * 146
58 * 96 = 32 * 174
63 * 74 = 18 * 259
64 * 79 = 32 * 158
73 * 96 = 12 * 584
76 * 98 = 14 * 532
11


Implementation of Java full permutation algorithm---small example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.