1 /**
2 * 吸血鬼数字:指位数为偶数,可以由一对数字相乘得出,这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排列。
3 * 以两个0结尾的数字不允许。
4 * 例如:
5 * 1260=21*60
6 * 1827=21*87
7 * 2187=27*81
8 * ...
9 * 写程序,找出4位数的所有吸血鬼数字。
10 */
11 import java.util.Arrays;
12
13 class XiXueGuiNum {
14 int num;
15 String a = null;
16
17 public void getNum() {
18 for (int i = 11; i <= 99; i++) {
19 for (int j = 99; j >= i; j--) {
20 num = i * j;
21 a = num + "";
22 // 保证乘积为4位数
23 if (a.length() == 4) {
24 // 将乘数拆分为2个个位数
25 String[] str1 = (i + "").split("");
26 String[] str2 = (j + "").split("");
27 // 判断乘积中是否包含乘数中各数字
28 if ((a.contains(str1[0])) && (a.contains(str1[1])) && (a.contains(str2[0]))
29 && (a.contains(str2[1]))) {
30 // 要求乘积不能以00结尾
31 if (!a.endsWith("00")) {
32 String[] name1 = a.split("");
33 Arrays.sort(name1);
34 String[] arr = { str1[0], str1[1], str2[0], str2[1] };
35 Arrays.sort(arr);
36 if (Arrays.equals(name1, arr)) {
37 // 乘积中的数字必须为4个不同数字
38 System.out.println(a + "=" + i + "*" + j);
39 }
40 }
41 }
42 }
43 }
44 }
45 }
46 }
47
48 public class Test4_10 {
49
50 public static void main(String[] args) {
51 long start = System.currentTimeMillis(); // 获取开始时间
52 // 要测试的程序或方法
53 XiXueGuiNum x = new XiXueGuiNum();
54 x.getNum();
55 long end = System.currentTimeMillis(); // 获取结束时间
56 System.out.println("程序运行时间: " + (end - start) + "ms");
57
58 }
59 }