Find out "the number of vampires" (Java, bubble sort)

Vampire numbers refer to numbers with even digits, which can be obtained by multiplying a pair of digits, and each pair of digits contains half the digits of the product. The digits selected from the initial digits can be sorted arbitrarily. Numbers ending in two 0s are not allowed. For example, the following numbers are all "vampire" numbers:
1260=21 * 60
1827=21 * 87
2187= 27 * 81

So, start

Since judging whether a four-digit number can be decomposed into the product of two two-digit numbers is much more troublesome than judging the four-digit number obtained by the product of two-digit numbers, the idea is the latter.

import java.util.ArrayList;
 import java.util.Arrays; 

/** 
 * Start from 1001 to 9999 to determine whether it can be decomposed into two two-digit numbers, and if so, whether the two-dimensional number contains all four numbers 
 * It is too troublesome to decompose a four-digit number into a two-digit product, and there may be multiple decomposition methods for a four-digit number, which increases the difficulty of the design 
 * But conversely, it is much simpler to multiply a two-digit number by a two-digit number. Go to judge again 
 * to solve the integral into four digits and store it in the 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 method 
    public  void sort( int [] array) {
         for ( int i = 0; i <array.length-1; i++ ) {
             for (int j = i+1; j <array.length; j++ ) {
                 if (array[i]> array[j]) {
                     // Exchange the positions of two numbers 
                    array[i] = array[i] -array[j ]; 
                    array[j] = array[i] + array[j]; 
                    array[i] = array[j] -array[i]; 
                } 
            } 
        } 
    } 
    
    /** 
     * Determine the four-digit number after decomposition (exist In the array), whether it contains all the digits of the two multiplication factors 
     * For simplicity, the method is to sort first, and then compare whether the two arrays are the same 
     */ 
    public  boolean check( int [] digits, int num1,int num2) {
         this .sort(digits);
 //         System.out.println(Arrays.toString(digits)); 
        
        int [] factors = new  int [4 ];
         // separate the two digits of the first multiplier 
        int [] numArray = this .divideNumber(num1);
         for ( int i = 0; i <2; i++ ) { 
            factors[i] = numArray[i]; 
        } 
        // separate the two digits of the second multiplier 
        numArray = 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; 
    } 
    ArrayList <Integer> result = new ArrayList<Integer> (); 

    /** 
     * 10*99 is less than 1000, so the loop starts from 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) {
                     // According to the title, if the last two
                     digits are 0, you cannot continue ; 
                } 
                int [] digits = divideArray(i* j);
                 if ( this .check(digits, i, j)) {
                     if ( this .result .contains(i* j))
                         continue ;
                     this .result.add(i* j); 
                    System.out.printf( "Number of %d vampires: %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:

第1个吸血鬼数: 1395 = 15 x 93 
第2个吸血鬼数: 1260 = 21 x 60 
第3个吸血鬼数: 1827 = 21 x 87 
第4个吸血鬼数: 2187 = 27 x 81 
第5个吸血鬼数: 1530 = 30 x 51 
第6个吸血鬼数: 1435 = 35 x 41 
第7个吸血鬼数: 6880 = 80 x 86 

Summary: reverse thinking, modular design

Guess you like

Origin blog.csdn.net/qq_45533926/article/details/112851322