[20-05-03][Self-test 36]Java Vampire Number

 1 package test_1_1;
 2 
 3 public class VampireNum {
 4 
 5     public static void main(String[] args) {
 6 
 7         /**
 8          * 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘得到
 9          * 这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序
10          * 以两个0结尾的数字是不允许的,例如:
11          * 1260 = 21 * 60
12          * 1827 = 21 * 87
13          * 找到4位数所有吸血鬼数
14          */
15         System.out.println("吸血鬼数包括:");
16         
17         for (int i = 1000; i < 10000; i++) {
18             judgeVampireNum(i);
19         }
20     }
21 
22     private static void judgeVampireNum(int num) {
23 
24         char[] chaArr = ("" + num).toCharArray();
25         int zeroCount = 0;
26         
27         int thou = (int)chaArr[0] - 48;
28         int hun = (int)chaArr[1] - 48;
29         int dec = (int)chaArr[2] - 48;
30         int unit = (int)chaArr[3] - 48;
31         
32         if (hun == 0) {
33             zeroCount++;
34         }
35         if (dec == 0) {
36             zeroCount++;
37         }
38         if (unit == 0) {
39             zeroCount++;
40         }
41         
42         if (zeroCount > 1) {
43             return;
44         }
45         
46         isVampireNum(thou, hun, dec, unit, num);
47         isVampireNum(thou, dec, hun, unit, num);
48         isVampireNum(thou, unit, hun, dec, num);
49         
50     }
51 
52     private static void isVampireNum(int num1, int num2, int num3, int num4, int num) {
53 
54         if ((num1 * 10 + num2) * (num3 * 10 + num4) == num) {
55             System.out.println(num + " = " + (num1 * 10 + num2) + " * " + (num3 * 10 + num4));
56         }
57         
58         if ((num2 * 10 + num1) * (num3 * 10 + num4) == num) {
59             System.out.println(num + " = " + (num2 * 10 + num1) + " * " + (num3 * 10 + num4));
60         }
61         
62         if ((num1 * 10 + num2) * (num4 * 10 + num3) == num) {
63             System.out.println(num + " = " + (num1 * 10 + num2) + " * " + (num4 * 10 + num3));
64         }
65         
66         if ((num2 * 10 + num1) * (num4 * 10 + num3) == num) {
67             System.out.println(num + " = " + (num2 * 10 + num1) + " * " + (num4 * 10 + num3));
68         }
69         
70     }
71 
72 }

结果如下:

吸血鬼数包括:
1260 = 21 * 60
1395 = 15 * 93
1435 = 41 * 35
1530 = 51 * 30
1827 = 21 * 87
2187 = 27 * 81
6880 = 86 * 80
6880 = 86 * 80

猜你喜欢

转载自www.cnblogs.com/mirai3usi9/p/12822543.html