Vampire number refers to the digital bits an even number, and may be obtained by multiplying by a pair of numbers, each of which contains half of the product of the number of bits of digital numbers, which select from the initial digital numbers can be arbitrary order. Two end digital 0 is not allowed, for example, the following figures are "vampire" numbers:
1260 = 21 * 60
1827 = 21 * 87
2187 * 81 = 27
So, start
Since the four-digit number can be determined whether the product of two digits into two, the resulting product of the ratio of four-digit digit determination, much trouble, so the idea of the latter.
Import of java.util.ArrayList; Import java.util.Arrays; / ** * start 1001 to 9999 start start determination, whether two digits into two, if so, whether the number of two-dimensional digital contains all four * broken down into the four-digit double-digit multiplication too much trouble, and there may be a variety of four-digit decomposition methods to increase the difficulty of the design * However, in turn, multiplied by two-digit number of digits is much simpler, this time you can go to determine * the integration solution into four digits, into an array. Two multipliers respectively into two digits, into an array * sorting the two arrays, one by one according to the following standard, and if the two arrays are identical, returns to true * @author zhaoke * * / public class Vampire { // take bubble sort public void Sort ( int [] Array) { for ( int I = 0; I <-be array.length. 1; I ++ ) { for (int J = I +. 1; J <be array.length; J ++ ) { IF (Array [I]> Array [J]) { // positions exchanged two numbers Array [I] = Array [I] - Array [J ]; Array [J] = Array [I] + Array [J]; Array [I] = Array [J] - Array [I]; } } } } / ** digit (the digit after determining the presence of decomposition * array), contains exactly whether all the digits of the two multiplication factor * for simplicity, the method is first sorted and then compare the two arrays are the same * / public Boolean Check ( int [] digits, int num1,int num2) { the this .sort (digits); // System.out.println (of Arrays.toString (digits)); int [] = Factors new new int [. 4 ]; // separating the first two digits of a multiplier int [] = numarray the this .divideNumber (num1); for ( int I = 0; I <2; I ++ ) { Factors [I] = numarray [I]; } // two separate digits of the second multiplier numarray = the this .divideNumber (num2); for ( int I = 2; I <. 4; I ++ ) { factors[i] = numArray[i-2]; } this.sort(factors); // System.out.println(Arrays.toString(factors)); for (int i = 0; i < digits.length; i++) { if (digits[i] != factors[i]) { return false; } } return true; } public int[] divideNumber(int number) { int[] digits = new int[2]; digits[0] = number/10; digits[1] = number - 10*(number/10); return digits; } /** * 获得每个位的数字 */ public int[] divideArray(int number) { int[] digits = new int[4]; int factor = 1000; for (int i = 0; i < digits.length; i++) { digits[i] = number/factor; number -= digits[i] * factor; factor /= 10; } Return digits; } the ArrayList <Integer> = Result new new the ArrayList <Integer> (); / ** * 10 * 99 is less than 1000, and the loop from the beginning. 11 * / public void Start () { int COUNT = 0; // counter only for ( int I =. 11; I <100; I ++ ) { for ( int J =. 11; J <100; J ++ ) { IF (I * J <1000 ) Continue ; IF (I * J% 100 == 0) { // The title, if the last two bits are 0, nor Continue ; } int [] = divideArray digits (I * J); IF ( the this .check (digits, I, J)) { IF ( the this .Result .Contains (I * J)) Continue ; the this .result.add (I * J); System.out.printf ( "vampire number.% d:% d =% dx% d \ n", ++ count, * I J, I, J); } } } } public static void main(String[] args) { Vampire v = new Vampire(); v.start(); } }
The results are as follows:
1st vampire number: 1395 = 15 x 93 second vampire number: 1260 = 21 x 60 3rd vampire number: 1827 = 21 x 87 4th vampire number: 2187 = 27 x 81 5th vampire number: 1530 = 30 x 51 6th vampire number: 1435 = 35 x 41 seventh vampire number: 6880 = 80 x 86
Summary: reverse thinking, modular design