Reprint: http://blog.csdn.net/tianmijieguo/article/details/46400911
From "Thinking in Java" (fourth edition) of the 4th chapter of exercise 10 See "Vampire number", the special programming implementation, the following are 3 algorithms (for four-digit) and its comparison:
Let's first explain the vampire numbers: vampire numbers are digits that are even digits, and can be multiplied by a pair of numbers, which is not allowed for numbers that contain the half digits of the product, ending with two 0.
Four-digit Vampire digital example: 1260=21*60,1827=21*87,2187=27*81 ...
First list the results: altogether 7:1260=21*60,1395=15*93,1435=41*35,1530=51*30,1827=87*21,2187=27*81,6880=86*80
Method One:
This method is the official answer to "thinking in Java", because the chapter is very close to the front, so the use of the four-digit traversal method, positive thinking, that is, first four digits, then split, four combination of numbers, if the product is equal to the original number, then output, and calculated as a vampire number.
[Java] View Plain copy public class searchforvampirethinkinginjava { // control/VampireNumbers.java // tij4 Chapter Control, Exercise 10, page 154 /* a vampire number has an even number of digits and is formed by multiplying a * pair of numbers containing half the number of digits of the result. the * digits are taken from the original number in any order. Pairs of trailing * zeroes are not allowed. Examples include: 1260 = 21 * 60, 1827 = 21 * 87, * 2187 = 27 * 81. write a program that finds all the 4-digit vampire numbers. * (Suggested by dan forhan.) */ // This method is a straightforward thinking, that is, first four digits, then split, four number combination multiplied, if the product and the original number is equal, then output, And counted as a vampire number. TMJG Add this row and comment // The result of Sum is 107,976 times, very large, the algorithm is inefficient, and there is a duplication (6880 = 86 * 80,6880 = 80 * 86). TMJG Add this row and comment static int sum=0;//the number of times the call is judged, TMJG add this row and comment static int a (int i) { return i/1000; //for thousand digits, below, TMJG add this line and comment } static int b (int i) { return (i%1000)/100; } static int c (int i) { return ((i%1000)%100)/10; } static int d (int i) { return ((i%1000)%100)%10; } static int com (int i, int j) { //formation 10~ 99 double digits, TMJG add this row and comment return (i * 10) + j; } static void productTest (int i, int m, int n) { sum++; if (m *&Nbsp;n == i) system.out.println (i + " = " + m + " * " + n"; } public static void main (String[] args) {