- String[] ar_str1, ar_str2;
- int sum = 0;
- int from;
- int to;
- int i_val;
- int count = 0;
- // 双重循环穷举
- for (int i = 10; i < 100; i++) {
- // j=i+1避免重复
- for(j = i+1 ; j< 100 ; j++){
-
- //from = Math.max(1000 / i, i + 1);
- //to = Math.min(10000 / i, 100);
- //for (int j = from; j < to; j++) { //没看懂
- i_val = i * j;
- // 下面的这个代码,我个人并不知道为什么,汗颜
-
if (i_val % 100 == 0 || (i_val - i - j) % 9 != 0) {
//假设val = 1000a + 100b + 10c + d, 因为满足val = x * y, 则有x = //10a + b, y = 10c + d
//则val - x - y = 990a + 99b + 9c = 9 * (110a + 11b + c), 所以val - x - y能被9整除。
//所以满足该条件的数字必定能被9整除,所以可以直接过滤其他数字。 精简算法 - continue;
- }
- count++;
- ar_str1 = String.valueOf(i_val).split("");
- ar_str2 = (String.valueOf(i) + String.valueOf(j)).split("");
- Arrays.sort(ar_str1);
- Arrays.sort(ar_str2);
- if (Arrays.equals(ar_str1, ar_str2)) {// 排序后比较,为真则找到一组
- sum++;
- System.out.println("wll 第" + sum + "组: " + i + "*" + j + "=" + i_val);
- }
- }
- }
- System.out.println("wll 共找到" + sum + "组吸血鬼数");
- System.out.println(count);
- }
I/System.out(10177): wll 第1组: 15*93=1395
I/System.out(10177): wll 第2组: 21*60=1260
I/System.out(10177): wll 第3组: 21*87=1827
I/System.out(10177): wll 第4组: 27*81=2187
I/System.out(10177): wll 第5组: 30*51=1530
I/System.out(10177): wll 第6组: 35*41=1435
I/System.out(10177): wll 第7组: 80*86=6880
I/System.out(10177): wll 共找到7组吸血鬼数
I/System.out(10177): wll279
I/System.out(10177): wll 第2组: 21*60=1260
I/System.out(10177): wll 第3组: 21*87=1827
I/System.out(10177): wll 第4组: 27*81=2187
I/System.out(10177): wll 第5组: 30*51=1530
I/System.out(10177): wll 第6组: 35*41=1435
I/System.out(10177): wll 第7组: 80*86=6880
I/System.out(10177): wll 共找到7组吸血鬼数
I/System.out(10177): wll279
如果用下面这个
- //from = Math.max(1000 / i, i + 1);
- //to = Math.min(10000 / i, 100);
- //for (int j = from; j < to; j++) { //没看懂
I/System.out( 9981): wll 共找到7组吸血鬼数
I/System.out(10085): wll 第1组: 15*93=1395
I/System.out(10085): wll 第2组: 21*60=1260
I/System.out(10085): wll 第3组: 21*87=1827
I/System.out(10085): wll 第4组: 27*81=2187
I/System.out(10085): wll 第5组: 30*51=1530
I/System.out(10085): wll 第6组: 35*41=1435
I/System.out(10085): wll 第7组: 80*86=6880
I/System.out(10085): wll 共找到7组吸血鬼数
I/System.out(10085): wll232
I/System.out(10085): wll 第1组: 15*93=1395
I/System.out(10085): wll 第2组: 21*60=1260
I/System.out(10085): wll 第3组: 21*87=1827
I/System.out(10085): wll 第4组: 27*81=2187
I/System.out(10085): wll 第5组: 30*51=1530
I/System.out(10085): wll 第6组: 35*41=1435
I/System.out(10085): wll 第7组: 80*86=6880
I/System.out(10085): wll 共找到7组吸血鬼数
I/System.out(10085): wll232