吸血鬼数字

来源:互联网 发布:淘宝 论坛 编辑:程序博客网 时间:2024/04/27 21:01

吸血鬼数字的三种算法:

第一种:

public class VampireNum {
static int a(int i) {
return i/1000;
}
static int b(int i) {
return (i%1000)/100;
}
static int c(int i) {
return ((i%1000)%100)/10;
}
static int d(int i) {
return ((i%1000)%100)%10;
}
static int com(int i, int j) {
return (i * 10) + j;
}
static void productTest (int i, int m, int n) {
if(m * n == i) System.out.println(i + " = " + m + " * " + n);
}
public static void main(String[] args) {
for(int i = 1001; i < 9999; i++) {
productTest(i, com(a(i), b(i)), com(c(i), d(i)));
productTest(i, com(a(i), b(i)), com(d(i), c(i)));
productTest(i, com(a(i), c(i)), com(b(i), d(i)));
productTest(i, com(a(i), c(i)), com(d(i), b(i)));
productTest(i, com(a(i), d(i)), com(b(i), c(i)));
productTest(i, com(a(i), d(i)), com(c(i), b(i)));
productTest(i, com(b(i), a(i)), com(c(i), d(i)));
productTest(i, com(b(i), a(i)), com(d(i), c(i)));
productTest(i, com(b(i), c(i)), com(d(i), a(i)));
productTest(i, com(b(i), d(i)), com(c(i), a(i)));
productTest(i, com(c(i), a(i)), com(d(i), b(i)));
productTest(i, com(c(i), b(i)), com(d(i), a(i)));
}
}
}


第二种:

import java.util.Arrays;


public class Vampire {
public static void main(String[] arg) {  
   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避免重复  
     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) {  
         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("第" + sum + "组: " + i + "*" + j + "=" + i_val);  
       }  
     }  
   }  
   System.out.println("共找到" + sum + "组吸血鬼数");  
   System.out.println(count);  
}  
}

第三种:

public class VampireNumbers {
public static void main(String[] args) {
   int[] startDigit = new int[4];
   int[] productDigit = new int[4];
   for(int num1 = 10; num1 <= 99; num1++)
     for(int num2 = num1; num2 <= 99; num2++) {
      
       if((num1 * num2) % 9 != (num1 + num2) % 9)
         continue;
       int product = num1 * num2;
       startDigit[0] = num1 / 10;
       startDigit[1] = num1 % 10;
       startDigit[2] = num2 / 10;
       startDigit[3] = num2 % 10;
       productDigit[0] = product / 1000;
       productDigit[1] = product % 1000 / 100;
       productDigit[2] = product % 1000 % 100 / 10;
       productDigit[3] = product % 1000 % 100 % 10;
       int count = 0;
       for(int x = 0; x < 4; x++)
         for(int y = 0; y < 4; y++) {
           if(productDigit[x] == startDigit[y]) {
             count++;
             productDigit[x] = -1;
             startDigit[y] = -2;
             if(count == 4)
               System.out.println(num1 + " * " + num2
                 + " = " + product);
           }
         }
     }
 }
}

0 0
原创粉丝点击