Thinking in java吸血鬼数字问题

本文通过编程算法,探讨了在特定范围内寻找满足条件的乘积序列,并详细解释了算法背后的逻辑与实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. String[] ar_str1, ar_str2;  
  2.     int sum = 0;  
  3.     int from;  
  4.     int to;  
  5.     int i_val;  
  6.     int count = 0;  
  7.     // 双重循环穷举  
  8.     for (int i = 10; i < 100; i++) {  
  9.       // j=i+1避免重复  
  10.       for(j = i+1 ; j< 100 ; j++){

  11.       //from = Math.max(1000 / i, i + 1);  
  12.       //to = Math.min(10000 / i, 100);  
  13.       //for (int j = from; j < to; j++) {   //没看懂
  14.         i_val = i * j;  
  15.         // 下面的这个代码,我个人并不知道为什么,汗颜  
  16.         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整除,所以可以直接过滤其他数字。 精简算法
  17.           continue;  
  18.         }  
  19.         count++;  
  20.         ar_str1 = String.valueOf(i_val).split("");  
  21.         ar_str2 = (String.valueOf(i) + String.valueOf(j)).split("");  
  22.         Arrays.sort(ar_str1);  
  23.         Arrays.sort(ar_str2);  
  24.         if (Arrays.equals(ar_str1, ar_str2)) {// 排序后比较,为真则找到一组  
  25.           sum++;  
  26.           System.out.println("wll 第" + sum + "组: " + i + "*" + j + "=" + i_val);  
  27.         }  
  28.       }  
  29.     }  
  30.     System.out.println("wll 共找到" + sum + "组吸血鬼数");  
  31.     System.out.println(count);  
  32.   }  
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
如果用下面这个
  1.       //from = Math.max(1000 / i, i + 1);  
  2.       //to = Math.min(10000 / i, 100);  
  3.       //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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值