吸血鬼数字算法

转载自 http://yangl.iteye.com/blog/264634

Java代码 复制代码 收藏代码
  1. package org.idcn.jse;
  2. public class XiXieGui {
  3. /**
  4. * 吸血鬼数字算法
  5. * 如:12*60=1260
  6. * YangL.
  7. */
  8. public static void main(String[] args) {
  9. String[] ar_str1, ar_str2;
  10. int sum = 0;
  11. for (int i = 10; i < 100; i++) {
  12. for (int j = i + 1; j < 100; j++) {
  13. int i_val = i * j;
  14. if (i_val < 1000 || i_val > 9999)
  15. continue; // 积小于1000或大于9999排除,继续下一轮环
  16. ar_str1 = String.valueOf(i_val).split("");
  17. ar_str2 = (String.valueOf(i) + String.valueOf(j)).split("");
  18. java.util.Arrays.sort(ar_str1);
  19. java.util.Arrays.sort(ar_str2);
  20. if (java.util.Arrays.equals(ar_str1, ar_str2)) {
  21. // 排序后比较,为真则找到一组
  22. sum++;
  23. System.out.println("第" + sum + "组: " + i + "*" + j + "="
  24. + i_val);
  25. }
  26. }
  27. }
  28. System.out.println("共找到" + sum + "组吸血鬼数");
  29. }
  30. }

这个算法应该是比较简单的了。算法中用到String.valueOf(j)).split("");的方法来把数字转换为字符串,以拆分数字的方法用的很巧妙了。把数字的比较,转换为对字符串的比较,实在高明。

下面是我在《Thinking in Java, 2nd edition, Annotated Solution Guide Revision 1.0》中找到的答案,也就是Thinking in JAVA的官方答案书上的答案。不过个人感觉没有上面的算法好。

Java代码 复制代码 收藏代码
  1. //: c03:E11_Vampire.java  
  2. // Solution by Dan Forhan  
  3. import java.applet.*;  
  4. import java.awt.*;  
  5.  
  6. publicclass Vampire extends Applet {  
  7.     privateint num1, num2, product;  
  8.     privateint[] startDigit = newint[4];  
  9.     privateint[] productDigit = newint[4];  
  10.     privateint count = 0;  
  11.     privateint vampCount = 0;  
  12.     privateint x, y;  
  13.     publicvoid paint(Graphics g) {  
  14.       g.drawString("Vampire Numbers", 10, 20);  
  15.       g.drawLine(10, 22, 150, 22);  
  16.       // Positioning for output to applet:  
  17.       int column = 10, row = 35;  
  18.       for(num1 = 10; num1 <= 99; num1++)  
  19.         for(num2 = 10; num2 <= 99; num2++) {  
  20.           product = num1 * num2;  
  21.           startDigit[0] = num1 / 10;  
  22.           startDigit[1] = num1 % 10;  
  23.           startDigit[2] = num2 / 10;  
  24.           startDigit[3] = num2 % 10;  
  25.           productDigit[0] = product / 1000;  
  26.           productDigit[1] = (product % 1000) / 100;  
  27.           productDigit[2] = product % 1000 % 100/10;  
  28.           productDigit[3] = product % 1000 % 100%10;  
  29.           count = 0;  
  30.           for(x = 0; x < 4; x++)  
  31.             for(y = 0; y < 4; y++) {  
  32.               if (productDigit[x] == startDigit[y]){  
  33.                 count++;  
  34.                 productDigit[x] = -1;  
  35.                 startDigit[y] = -2;  
  36.                 if (count == 4) {  
  37.                   vampCount++;  
  38.                   int a = (int)Math.random() * 100;  
  39.                   int b = (int)Math.random() * 100;  
  40.                   int c = (int)Math.random() * 100;  
  41.                   if (vampCount < 10) {  
  42.                     g.drawString("Vampire number     " 
  43.                       + vampCount + " is " + num1 +  
  44.                       " * " + num2 + " = "+ product,  
  45.                       column, row);  
  46.                     row += 20;  
  47.                   } else {  
  48.                     g.drawString("Vampire number    " 
  49.                       + vampCount + " is " + num1 +  
  50.                       " * " + num2 + " = "+ product,  
  51.                       column, row);  
  52.                     row += 20;  
  53.                   }  
  54.                 }  
  55.               }  
  56.             }  
  57.         }  
  58.     }  
  59. } ///:~ 
原文地址:https://www.cnblogs.com/pengfeiliu/p/3722145.html