吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选择的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼”数字: 1260=21*60 1827=21*87 2187=27*81 写一个程序,找出4位数的所有吸血鬼数字Java代码
时间: 2024-04-06 14:35:06 浏览: 129
以下是一个Java程序,可以找出所有4位数的吸血鬼数字:
```java
public class VampireNumbers {
public static void main(String[] args) {
for (int i = 1000; i < 10000; i++) {
int a = i / 1000;
int b = (i / 100) % 10;
int c = (i / 10) % 10;
int d = i % 10;
if (checkVampireNumber(a, b, c, d, a, b, c, d)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, a, b, d, c)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, a, c, b, d)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, a, c, d, b)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, a, d, b, c)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, a, d, c, b)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, b, a, c, d)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, b, a, d, c)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, b, c, a, d)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, b, c, d, a)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, b, d, a, c)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, b, d, c, a)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, c, a, b, d)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, c, a, d, b)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, c, b, a, d)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, c, b, d, a)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, c, d, a, b)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, c, d, b, a)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, d, a, b, c)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, d, a, c, b)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, d, b, a, c)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, d, b, c, a)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, d, c, a, b)) {
System.out.println(i);
}
if (checkVampireNumber(a, b, c, d, d, c, b, a)) {
System.out.println(i);
}
}
}
public static boolean checkVampireNumber(int a, int b, int c, int d, int w, int x, int y, int z) {
int num1 = 10 * a + b;
int num2 = 10 * c + d;
int product = num1 * num2;
int firstHalf = 10 * w + x;
int secondHalf = 10 * y + z;
return product == firstHalf * secondHalf && product % 100 != 0;
}
}
```
该程序通过枚举所有可能的数字组合,并检查它们是否满足吸血鬼数字的定义来找出所有4位数的吸血鬼数字。
阅读全文