java编程——吸血鬼数字(四位)


转载:http://blog.csdn.net/tianmijieguo/article/details/46400911


从《Thinking in Java》(中文第四版)中第4章的练习10看到“吸血鬼数字”,特编程实现,以下为3种算法(针对四位数的)及其对比:

首先解释一下吸血鬼数字:吸血鬼数字是指位数为偶数的数字,可由一对数字相乘而得到,这对数字各包含乘积的一半位数的数字,以两个0结尾的数字是不允许的。

 四位数吸血鬼数字示例:1260=21*60,1827=21*87,2187=27*81……

先列出结果:一共7个:1260=21*60,1395=15*93,1435=41*35,1530=51*30,1827=87*21,2187=27*81,6880=86*80


方法一:

本方法是《Thinking in Java》的官方答案,由于所处章节很靠前,所以采用的遍历四位数方法,正向思维,即先有四位数,再拆分,四个数字组合相乘,若乘积与原数相等,则输出,并计算为一个吸血鬼数字。

[java]  view plain  copy
  1. public class SearchforVampireThinkinginJava {  
  2.   
  3.     // control/VampireNumbers.java  
  4.     // TIJ4 Chapter Control, Exercise 10, page 154  
  5.     /* A vampire number has an even number of digits and is formed by multiplying a 
  6.     * pair of numbers containing half the number of digits of the result. The 
  7.     * digits are taken from the original number in any order. Pairs of trailing 
  8.     * zeroes are not allowed. Examples include: 1260 = 21 * 60, 1827 = 21 * 87, 
  9.     * 2187 = 27  * 81. Write a program that finds all the 4-digit vampire numbers. 
  10.     * (Suggested by Dan Forhan.) 
  11.     */   
  12. //  本方法是顺向思维,即先有四位数,再拆分,四个数字组合相乘,若乘积与原数相等,则输出,并计算为一个吸血鬼数字。TMJG添加此行并注释  
  13. //  其实sum的结果为107976次,非常大,算法效率很低,并且出现了重复(6880 = 86 * 80,6880 = 80 * 86)。TMJG添加此行并注释  
  14.       
  15.             static int sum=0;//记录调用判断的次数,TMJG添加此行并注释  
  16.             static int a(int i) {  
  17.                 return i/1000;    //求千位数字,下同,TMJG添加此行并注释  
  18.             }  
  19.             static int b(int i) {  
  20.                 return (i%1000)/100;  
  21.             }  
  22.             static int c(int i) {  
  23.                 return ((i%1000)%100)/10;  
  24.             }  
  25.             static int d(int i) {  
  26.                 return ((i%1000)%100)%10;  
  27.             }  
  28.             static int com(int i, int j) {   //形成10~99的两位数,TMJG添加此行并注释  
  29.                 return (i * 10) + j;  
  30.             }  
  31.             static void productTest (int i, int m, int n) {  
  32.                 sum++;  
  33.                 if(m * n == i) System.out.println(i + " = " + m + " * " + n);  
  34.             }     
  35.         public static void main(String[] args) {          
  36.             for(int i = 1001; i < 9999; i++) {             
  37.                 productTest(i, com(a(i), b(i)), com(c(i), d(i)));  
  38.                 productTest(i, com(a(i), b(i)), com(d(i), c(i)));  
  39.                 productTest(i, com(a(i), c(i)), com(b(i), d(i)));  
  40.                 productTest(i, com(a(i), c(i)), com(d(i), b(i)));  
  41.                 productTest(i, com(a(i), d(i)), com(b(i), c(i)));  
  42.                 productTest(i, com(a(i), d(i)), com(c(i), b(i)));  
  43.                 productTest(i, com(b(i), a(i)), com(c(i), d(i)));  
  44.                 productTest(i, com(b(i), a(i)), com(d(i), c(i)));  
  45.                 productTest(i, com(b(i), c(i)), com(d(i), a(i)));  
  46.                 productTest(i, com(b(i), d(i)), com(c(i), a(i)));  
  47.                 productTest(i, com(c(i), a(i)), com(d(i), b(i)));  
  48.                 productTest(i, com(c(i), b(i)), com(d(i), a(i)));  
  49.             }     
  50.             System.out.println("总共调用判断的次数为:"+sum);//TMJG添加此行并注释  
  51.         }   
  52. }  

方法二:

本方法是对方法一的改进,跳过了一些数字(如1100这样末尾两个0的,如1010、1001这样明显不可能是吸血鬼数字的数字),并且避免了出现重复的可能性,但是效率仍然很低,需要判断104942次。

[java]  view plain  copy
  1. public class SearchforVampireNumbersLJ {  
  2.     public static int count = 0;// 记录一共有多少个吸血鬼数字  
  3.     public static int k=0;//记录调用判断多少次  
  4.     public static void main(String[] args) {  
  5.         for (int i = 1001; i < 10000; i++) {  
  6.             // 如果数字是像1100这种末尾至少有2个0的,则跳过  
  7.             if (i % 100 == 0) {  
  8.                 continue;  
  9.             }  
  10.             // 获得数字四个数值位上的数字,这里我们假定四位数表示为abcd  
  11.             int a = i / 1000;  
  12.             int b = (i - a * 1000) / 100;  
  13.             int c = (i - a * 1000 - b * 100) / 10;  
  14.             int d = i - a * 1000 - b * 100 - c * 10;  
  15.             // 判断四个位置上是否有两个0存在的情况,如1010,并跳过这些数,由于千位不可能为0,因此只需要判断另外三位是否有2个0的情况  
  16.             // 当3个数中有2个0时,必然存在“3个数之和等于其中某一个数”,以此作为判断依据,而后两位为0的也已经排除,其实只需要判断如1001,和1010这种情况即可  
  17.             if (b + c + d == c || b + c + d == d) {  
  18.                 continue;  
  19.             }  
  20.             // 排除掉各种情况后,可以开始真正的吸血鬼数字筛选了  
  21.             // 那么一共有12种情况:abcd,abdc,acbd,acdb,adbc,adcb,bacd,badc,bcda,bdca,cadb,cbda  
  22.             if (search(i, a, b, c, d)) {  
  23.             } else if (search(i, a, b, d, c)) {  
  24.             } else if (search(i, a, c, b, d)) {  
  25.             } else if (search(i, a, c, d, b)) {  
  26.             } else if (search(i, a, d, b, c)) {  
  27.             } else if (search(i, a, d, c, b)) {  
  28.             } else if (search(i, b, a, c, d)) {  
  29.             } else if (search(i, b, a, d, c)) {  
  30.             } else if (search(i, b, c, d, a)) {  
  31.             } else if (search(i, b, d, c, a)) {  
  32.             } else if (search(i, c, a, d, b)) {  
  33.             } else if (search(i, c, b, d, a)) {  
  34.             }  
  35.         }  
  36.         System.out.println("四位数的吸血鬼数字一共有" + count + "个。");  
  37.         System.out.println("一共调用判断次数为" + k);  
  38.     }  
  39.   
  40.     //判断是否满足条件  
  41.     static  boolean search(int i, int a, int b, int c, int d) {  
  42.         k++;  
  43.         if ((a * 10 + b) * (c * 10 + d) == i) {  
  44.             searchfor(i,a,b,c,d);//如果满足条件,则打印结果  
  45.             return true;  
  46.         }else{  
  47.             return false;  
  48.         }  
  49.     }  
  50.   
  51.     //满足条件即打印,并且统计个数  
  52.     static void searchfor(int i, int a, int b, int c, int d) {  
  53.         count++;  
  54.         System.out.println(i + "=" + (a * 10 + b) + "*" + (c * 10 + d));  
  55.     }  
  56. }  

方法三:

以下是网上找的代码,该算法采用逆向思维,4位数字的吸血鬼数字只能拆分成两个2位数,因此遍历所有两个两位数相乘的情况,除去不符合的情况不用判断,其他挨个判断即可。

[java]  view plain  copy
  1. public class SearchforVampireFromInternet {  
  2.         /**    
  3.          * 代码来自网络,略作修改并添加了注释 
  4.          * 该算法只需要执行3721次 
  5.          */     
  6.         public static void main(String[] args) {     
  7.             String[] targetNum = null;  
  8.             String[] gunNum=null;   //目标数字和枪数字(即对比数字)  
  9.             int sum = 0;   //吸血鬼数字的总个数  
  10.             int count=0;   //有效判断次数,那些乘积不是4位数的就排除了  
  11.             for (int i = 10; i < 100; i++) {     
  12.                 for (int j = i+1; j < 100; j++) {   //没有哪个两位数满足ab*ab=abab(不信可以编程验证),所以这里j从i+1开始就可以了  
  13.                     int i_target = i * j;     
  14.                     if (i_target < 1000 || i_target > 9999)     
  15.                         continue// 积不是4位数则跳过   
  16.                     count++;  
  17.                     targetNum = String.valueOf(i_target).split("");  //将其转换为一个字符串数组  
  18.                     gunNum = (String.valueOf(i) + String.valueOf(j)).split("");     
  19.                     java.util.Arrays.sort(targetNum);   //升序排列,因为只有排列了再比较才能保证不遗漏abcd=ba*dc这样的情况  
  20.                     java.util.Arrays.sort(gunNum);     
  21.                     if (java.util.Arrays.equals(targetNum, gunNum)) {     
  22.                         // 排序后比较,为真则找到一组     
  23.                         sum++;     
  24.                         System.out.println("第" + sum + "个: " +  i_target+"="+i + "*" + j);     
  25.                     }     
  26.                 }     
  27.             }     
  28.             System.out.println("共进行了"+count+"次判断,找到" + sum + "个吸血鬼数字。");  
  29.         }     
  30. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值