Directory
1 Problem Description
2 Solutions
1 problem description
Look at the following equation:
--x--=--x---
It means: two two-digit multiplication equals a two-digit number multiplied by a
Three-digit number.
If there are no qualifying conditions, there are many examples.
But the current limit is: These 9 squares, 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
2 Solutions
1 Importjava.util.ArrayList;2 3 Public classMain {4 Public Staticarraylist<string> list =NewArraylist<string>();5 6 Public voidSwapint[] A,intIintj) {7 inttemp =A[i];8A[i] =A[j];9A[J] =temp;Ten } One A Public voidCheckint[] A) { - intA = a[0] * ten + a[1]; - intb = a[2] * ten + a[3]; the intc = a[4] * ten + a[5]; - intD = a[6] * + a[7] * ten + a[8]; - if(A >b) { - inttemp =A; +A =b; -b =temp; + } A if(A * b = = c *d) { atStringBuffer s =NewStringBuffer (""); - S.append (a); -S.append ("X"); - S.append (b); -S.append ("="); - S.append (c); inS.append ("X"); - S.append (d); to if(!List.contains (s.tostring ())) + List.add (s.tostring ()); - } the } * $ Public voidDfsint[] A,intStep) {Panax Notoginseng if(Step = =a.length) { - check (A); the return; +}Else { A for(inti = Step;i < a.length;i++) { the swap (A, I, step); +DFS (A, step + 1); - swap (A, I, step); $ } $ } - } - the Public Static voidMain (string[] args) { -Main test =NewMain ();Wuyi int[] A = {1,2,3,4,5,6,7,8,9}; theTest.dfs (A, 0); - for(inti = 0;i < List.size (); i++) Wu System.out.println (List.get (i)); - } About}
Operation Result:
x = x 15854 x 13854 x = x x 18658 x 17458 = x 13458 x = ~ x 14658 x [x] * * * 17463 x = x 25964 x = 532 x 15873 x = x 58476 x 98 = x
Algorithmic note _220: Guessing formula (Java)