A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马张国礼 初级黑马   /  2012-6-12 20:08  /  3076 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看java编程思想的时候看到这么一个题,与大家分享一下。吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这个数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼”数字:
1260=21*60
1827=21*87
2187=27*81
挺有意思的题目,大家一块学习一下

评分

参与人数 1黑马币 +20 收起 理由
黄奕豪 + 20 赞一个!

查看全部评分

9 个回复

倒序浏览
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);   
  }   
}  

回复 使用道具 举报
张洁 发表于 2012-6-12 20:13
import java.util.Arrays;   
/**  
* 吸血鬼数字

百度固然方便,还是自己动手动脑一下,比较好吧,
回复 使用道具 举报
这个是四位的吸血鬼数字计算,虽然有点罗嗦,不过挺好玩的。
public class Xixueguinumber
{
      
      public int cal(int x, int y, int z, int w)
      {
          int num = (x * 10 + y) * (z * 10 + w);
          return num;
         
      }
     public static void main(String[] args)
     {
         for (int i = 1000; i <= 9999;i++)
         {
             int a = (int)(i/1000);
             int b = (int)((i - a*1000)/100);
             int c = (int)((i-((int)(i/100))*100)/10);
             int d = (int)(i - ((int)(i/10))*10);
             Xixueguinumber Xi = new Xixueguinumber();
             boolean b1 = Xi.cal(a,b,c,d) == i;
             boolean b2 = Xi.cal(b,a,c,d) == i;
             boolean b3 = Xi.cal(a,b,d,c) == i;
             boolean b4 = Xi.cal(b,a,d,c) == i;
             boolean b5 = Xi.cal(a,c,b,d) == i;
             boolean b6 = Xi.cal(a,c,d,b) == i ;
             boolean b7 = Xi.cal(c,a,b,d) == i;
             boolean b8 = Xi.cal(c,a,d,b) == i;
             boolean b9 = Xi.cal(a,d,b,c) == i;
             boolean b10 = Xi.cal(d,a,b,c) == i;
             boolean b11 = Xi.cal(a,d,c,b) == i;
             boolean b12 = Xi.cal(d,a,c,b) == i;
             Boolean array[] ={b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12};
             for(int j = 0; j <12; j++)
             {
                 if(array[j])
                 {
                     System.out.print("吸血鬼数字为:" + i + "=" );
                     switch(j+1)
                     {
                         case 1 : System.out.println(""+ a + b + " * " + c + d);break;
                         case 2 : System.out.println(""+ b + a + " * " + c + d);break;
                         case 3 : System.out.println(""+ a + b + " * " + d + c);break;
                         case 4 : System.out.println(""+ b + a + " * " + d + c);break;
                         case 5 : System.out.println(""+ a + c + " * " + b + d);break;
                         case 6 : System.out.println(""+ a + c + " * " + d + b);break;
                         case 7 : System.out.println(""+ c + a + " * " + b + d);break;
                         case 8 : System.out.println(""+ c + a + " * " + d + b);break;
                         case 9 : System.out.println(""+ a + d + " * " + b + c);break;
                         case 10 : System.out.println(""+ d + a + " * " + b + c);break;
                         case 11 : System.out.println(""+ a + d + " * " + c + b);break;
                         case 12 : System.out.println(""+ d + a + " * " + c + b);break;
                     }
                 }

             }

         }
     }
}
回复 使用道具 举报
史卜坤 发表于 2012-6-12 21:09
这个是四位的吸血鬼数字计算,虽然有点罗嗦,不过挺好玩的。
public class Xixueguinumber
{

Good!~  好直接的方法
回复 使用道具 举报

  1. public class XiXueGui {

  2.         public static void main(String[] args){
  3.                 int bits=1;// 例 A*B  bits 代表 A的位数
  4.                 int temp_i;
  5.                 int[] b =new int[10];
  6.                
  7.         //        System.out.println(b[5]);
  8.                 for(int i=0;i<1000;i++){
  9.                        
  10.                        
  11.                         temp_i = i;
  12.                         while(temp_i/10!=0){
  13.                                 temp_i/=10;
  14.                                 bits*=10;
  15.                         }
  16.         //                System.out.println(bits);
  17.                         for(int j=bits; j<bits*10; j++){
  18.                                 setfalse(b); //标记数组全部置为 false
  19.                                 setture(b,i*j);// 将答案拆分成一位一位的  在对应的 标记为置 true;
  20.                                 if(equal(b,i,j))
  21.                                         System.out.println(i+" * "+j+" = "+i*j);
  22.                         }
  23.                         bits=1;
  24.                 }
  25.         }
  26.        
  27.         //检测是否相等
  28.         private static boolean equal(int[] b,int i, int j){
  29.                 int temp = i*j;
  30.                 do{
  31.                         if(b[i%10]==0)
  32.                                 return false;
  33.                         b[i%10]--;
  34.                         i = i/10;
  35.                 }while(i!=0);
  36.                 do{
  37.                         if(b[j%10]==0)
  38.                                 return false;
  39.                         b[j%10]--;
  40.                         j = j/10;
  41.                 }while(j!=0);
  42.                 if(temp%100==0)//排除末尾为两个零
  43.                         return false;
  44.                 return true;
  45.         }
  46.        
  47.         //
  48.         private static void setture(int[] b,int num){
  49.                 do{
  50.                         b[num%10]+= 1;
  51.                         num = num/10;
  52.                 }while(num!=0);
  53.                
  54.         }
  55.        
  56.         //讲数组初 置 false
  57.         private static void setfalse(int[] b){
  58.                 for(int i=0; i<b.length; i++){
  59.                         b[i]=0;
  60.                 }
  61.         }
  62. }


  63. 不知道能否被看懂~  o(∩_∩)o 哈哈,只追求结果,所以没有考虑可读性,还好我也做了一点注解
复制代码

点评

判断相等的地方确实巧妙…… 我是利用ArrayList排序后,看两个集合的元素是否完全相等。  发表于 2012-6-13 12:19
回复 使用道具 举报
本帖最后由 何旭栋 于 2012-6-13 01:07 编辑
  1. <div class="blockcode"><blockquote>public class Test
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 vampire();
  6.         }

  7.         public static void vampire()
  8.         {
  9.                 int product = 0;
  10.                 for (int x = 100; x<1000; x++)
  11.                 {
  12.                         for (int y=x; y<1000; y++)
  13.                         {
  14.                                 product = x*y;
  15.                                 if (compare(product,x,y))
  16.                                 {
  17.                                         System.out.println(product+"="+x+"*"+y);
  18.                                 }
  19.                         }
  20.                 }
  21.         }
  22.        
  23.         //比较整数a和b,c的各个数字是否相同
  24.         public static boolean compare(int a, int b, int c)
  25.         {
  26.                 int[] product = toArray(a);
  27.                 int[] bc = addArray(toArray(b),toArray(c));
  28.                 if (product.length != bc.length)
  29.                 {
  30.                         return false;
  31.                 }
  32.                 else if (product[product.length-1]==0 && product[product.length-2]==0)
  33.                 {
  34.                         return false;
  35.                 }
  36.                 sortArray(product);
  37.                 sortArray(bc);
  38.                 for (int i=0; i<product.length; i++)
  39.                 {
  40.                         if (product[i]!=bc[i])
  41.                         {
  42.                                 return false;
  43.                         }
  44.                 }
  45.                 return true;
  46.         }

  47.         public static void sortArray(int[] arr)
  48.         {
  49.                 for (int i=0; i<arr.length-1; i++)
  50.                 {
  51.                         for (int j=1; j<arr.length; j++)
  52.                         {
  53.                                 int temp;
  54.                                 if (arr[i]>arr[j])
  55.                                 {
  56.                                         temp = arr[i];
  57.                                         arr[i] = arr[j];
  58.                                         arr[j] = temp;
  59.                                 }
  60.                         }
  61.                 }
  62.         }
  63. //把整数换成整形数组
  64.         public static int[] addArray(int[] a, int[] b)
  65.         {
  66.                 int al = a.length, bl = b.length;
  67.                 int[] add = new int[al+bl];
  68.                 for (int i=0; i<al; i++)
  69.                 {
  70.                         add[i] = a[i];
  71.                 }
  72.                 for (int i=al; i<al+bl; i++)
  73.                 {
  74.                         add[i] = b[i-al];
  75.                 }
  76.                 return add;
  77.         }
  78. //排序
  79.         public static int[] toArray(int x)
  80.         {
  81.                 int index = bits(x);
  82.                 int[] num = new int[index];
  83.                 for (int i = index-1; i>=0; i--)
  84.                 {
  85.                         num[i] = x%10;
  86.                         x = x/10;
  87.                 }
  88.                 return num;
  89.         }
  90. //x是多少位数
  91.         public static int bits(int x)
  92.         {
  93.                 int bits = 1;
  94.                 while(x/10!=0)
  95.                 {
  96.                         bits++;
  97.                         x = x/10;
  98.                 }
  99.                 return bits;
  100.         }
  101. }
复制代码
三位数的吸血鬼数好多啊,四位数就开始刷屏了
118440=141*840
136948=146*938
115672=152*761
146952=156*942
116725=161*725
156915=165*951
162976=176*926
129775=179*725
102510=201*510
126027=201*627。。。。

回复 使用道具 举报
郭宁 发表于 2012-6-12 22:36

这个方法好,佩服:handshake
回复 使用道具 举报
郭宁 中级黑马 2012-6-13 07:51:21
9#
何旭栋 发表于 2012-6-13 01:22
这个方法好,佩服

程序应该写成你这样的,有很好的可读性,也方便修改和优化。
回复 使用道具 举报
本帖最后由 罗文杰 于 2012-6-13 11:58 编辑

额 上午看到这道题弄了下,贴下我的代码,这里没用数组直接用ArrayList集合。
  1. public class VampireNumber {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 getVampireNumber(3);
  8.         }
  9.         /**
  10.          * 用来获得打印打印吸血鬼数字的函数
  11.          * 初始条件:无
  12.          * 结束条件:无
  13.          * @param int digit:乘数的数位位数
  14.          *        double numScope: 乘数的最大数字边界                           
  15.          */
  16.         public static void getVampireNumber(int digit){
  17.                 double numScope = Math.pow(10, digit);               
  18.                                 
  19.                 for (int i = (int)Math.pow(10, (digit - 1)); i < numScope; i++){
  20.                         for (int j = i; j < numScope; j++){                                
  21.                                 if(calculateNum(i, j) != 0){
  22.                                         System.out.println("乘数为"+digit+"位的吸血鬼数字为:"+calculateNum(i, j)+" = "
  23.                                                                                         + i + "*" +j);
  24.                                 }
  25.                         }
  26.                 }
  27.         }
  28.         /**
  29.          * 用来计算吸血鬼数字的函数
  30.          * 初始条件:无
  31.          * 结束条件:返回数字乘积product  
  32.          * @param int multiplierFir: 乘数一
  33.          *        int multiplierSec: 乘数二
  34.          *        int product: 乘积
  35.          *               
  36.          */
  37.         public static int calculateNum(int multiplierFir, int multiplierSec){
  38.                 ArrayList<Integer> alMultiplier = new ArrayList<Integer>();
  39.                 ArrayList<Integer> alProduct = new ArrayList<Integer>();
  40.                 int product = multiplierFir * multiplierSec;
  41.                 //将两个乘数的所有数字存入乘数集合alMultiplier中
  42.                 if ((multiplierFir % 10 != 0) || (multiplierSec % 10 != 0)){
  43.                         int mFirData = multiplierFir;
  44.                         int mSecData = multiplierSec;
  45.                         while ((mFirData != 0)&& (mSecData != 0)){
  46.                                 alMultiplier.add(mFirData % 10);
  47.                                 mFirData = mFirData / 10;
  48.                                 alMultiplier.add(mSecData % 10);
  49.                                 mSecData = mSecData / 10;                                
  50.                         }                        
  51.                 }
  52.                 //将两个数的成绩存入乘积集合alProduct中
  53.                 int temp = product;
  54.                 while(temp != 0){                        
  55.                         alProduct.add(temp % 10);
  56.                         temp = temp / 10;
  57.                 }                        
  58.                
  59.                 Collections.sort(alMultiplier);
  60.                 Collections.sort(alProduct);
  61.                 if(equals(alMultiplier,alProduct)){
  62.                         return product;
  63.                 }
  64.                 return 0;
  65.         }
  66.         /**
  67.          * 用来判断两个具有自然顺序的ArrayList<Integer>集合的所有元素是否相同
  68.          * 初始条件:无
  69.          * 结束条件:返回boolean flag的值
  70.          * @param ArrayList<Integer> alist1: 集合一
  71.          *        ArrayList<Integer> alist2: 集合二
  72.          *        boolean flag: 判断标记                        
  73.          */
  74.         
  75.         public static boolean equals(ArrayList<Integer> alist1, ArrayList<Integer> alist2){
  76.                 boolean flag = true;
  77.                 if(alist1.size() == alist2.size()){
  78.                         for (int i = 0; i < alist1.size(); i++){
  79.                                 if (alist1.get(i) != alist2.get(i)){
  80.                                         flag = false;
  81.                                         break;
  82.                                 }                                       
  83.                         }
  84.                         return flag;
  85.                 }
  86.                 return !flag;                                
  87.         }
  88. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马