- package org.study.sort;
- import java.util.Arrays;
- /**
- * 问题描述:
- * 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,
- * 其中从最初的数字中选取的数字可以任意排序。
- * 例如:
- * 1260 = 21 * 60 1827 = 21 * 87 2187 = 27 * 81
- * 要求输出所有四位数的吸血鬼数字。
- *
- * 注:参考了CSDN一朋友的写法
- */
- public class VampireNumber {
- public static void main(String[] args) {
- for(int i = 1; i < 100; i++){
- for(int j = i+1; j < 100; j++){
- //只要求输出四位数
- if(i * j >= 1000){
- String a = i + "" + j;
- String b = i * j + "";
- if(equal(a, b)){
- System.out.printf("%d * %d = %d", i, j, i*j);
- System.out.println();
- }
- }
- }
- }
- }
- //判断两个字符串包含的数字是否一致
- private static boolean equal(String a, String b) {
- //先排序
- char[] as = a.toCharArray();
- char[] bs = b.toCharArray();
- Arrays.sort(as); //排序
- Arrays.sort(bs); //排序
- if(Arrays.equals(as, bs)){
- return true;
- }
- return false;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/161220138063.html
来源: http://www.codesnippet.cn/detail/161220138063.html