吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼”数字:
1260 = 21 * 60
1827 = 21 * 87
2187 = 27 * 81
以下,找出4位数的所有吸血鬼数字。(一个思路)
public class test1 {
public static void main(String[] args) {
VampireNumber();
}
// 吸血鬼数字
public static void VampireNumber() {
for(int num1 = 10; num1 <= 99; num1 ++){
for(int num2 = num1; num2 <= 99; num2++){
int num = num1 * num2;
if(num < 1000) continue;
Integer input1 = num1 / 10;
Integer input2 = num1 % 10;
Integer input3 = num2 / 10;
Integer input4 = num2 % 10;
List<Integer> targets = new ArrayList<>();
targets.add(input1);
targets.add(input2);
targets.add(input3);
targets.add(input4);
Integer output1 = num / 1000;
Integer output2 = num % 1000 / 100;
Integer output3 = num % 1000 % 100 / 10;
Integer output4 = num % 1000 % 100 % 10;
if(targets.contains(output1)){
targets.remove(output1);
}
if(targets.contains(output2)){
targets.remove(output2);
}
if(targets.contains(output3)){
targets.remove(output3);
}
if(targets.contains(output4)){
targets.remove(output4);
}
if(targets.size() == 0){
System.out.println(num1 + " * " + num2 + " = " + num);
}
}
}
}
}