现在的位置: 首页 > 综合 > 正文

吸血数字

2013年08月06日 ⁄ 综合 ⁄ 共 2815字 ⁄ 字号 评论关闭
问题描述:

     所谓“吸血鬼数字”就是指位数为偶数的数字(我们算得是4位的),可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数字,其中从偶数位数字中选取的数字可以任意排列。以两个0截尾的数字是不允许的。例如:1260=21*60          1827=21*87          2187=27*81

 

实现如下:找出所有四位吸血数字:

1260=60*21  1395=15*93  1435=35*41  1530=30*51  1827=87*21  2187=27*81 6880=80*86

  1. package BloodSucker;
  2. public class BloodSucker {
  3.     static boolean check(int args)
  4.     {
  5.         String number[] = new String[4]; 
  6.         int temp;
  7.         String number1,number2;
  8.         number[0]= new Integer(args/1000).toString();//千位
  9.         temp=args%1000;
  10.         number[1]=new Integer(temp/100).toString();//百位
  11.         temp=temp%100;
  12.         number[2]=new Integer(temp/10).toString();//十位
  13.         number[3]=new Integer(temp%10).toString();//个位
  14.         
  15.         if(number[2]=="0" && number[3]=="0")
  16.             return false;
  17.         
  18.         for(int i=0;i<=3;i++)
  19.         {
  20.             for(int j=0;j<=3;j++)
  21.             {
  22.                 if(i!=j)
  23.                 {
  24.                 for(int m=0;m<3;m++)
  25.                     if(m!=i&&m!=j)
  26.                     {
  27.                     for(int n=0;n<3;n++)
  28.                     {
  29.                       if(n!=i&&n!=j&&n!=m)
  30.                       {
  31.                           
  32.                                            temp=Integer.parseInt(number[i]+number[j])*Integer.parseInt(number[m]+number[n]);
  33.                         if(temp==args)
  34.                         {
  35.                             
  36.                             System.out.println("args:"+args+"  "+number[i]+number[j]+"*"+number[m]+number[n]);
  37.                             return true;
  38.                         }
  39.                       }
  40.                     }
  41.                     }
  42.                 }
  43.             }
  44.     }
  45.         
  46.         
  47.         
  48.         return false;
  49.     }
  50.     public static void main(String args[])
  51.     {
  52.         for(int i=1001;i<=9999;i++)
  53.         {
  54.             if(check(i));
  55.                 //System.out.println(i);
  56.         }
  57.         System.out.println("END");
  58.         
  59.         
  60.     }
  61. }

下面是比较好的实现方法:值得推荐!!!

  1. class Eg0410_Vampire{
  2.         public static void main(String[] args){
  3.            String[] ar_str1,ar_str2; 
  4.            int sum=0
  5.            for(int i=10;i<100;i++){ 
  6.                for(int j=i+1;j<100;j++){ 
  7.                     int i_val=i*j; 
  8.                     if(i_val<1000||i_val>9999continue//积小于1000或大于9999排除,继续下一轮环 
  9.                     ar_str1=String.valueOf(i_val).split(""); //超级好的数字拆分方法
  10.                     ar_str2=(String.valueOf(i)+String.valueOf(j)).split(""); 
  11.                     java.util.Arrays.sort(ar_str1); 
  12.                     java.util.Arrays.sort(ar_str2); 
  13.                     if(java.util.Arrays.equals(ar_str1, ar_str2)){
  14.                     //排序后比较,为真则找到一组 
  15.                         sum++; 
  16.                         System.out.println("第"+sum+"组: "+i+"*"+j+"="+i_val); 
  17.                    } 
  18.               } 
  19.           } 
  20.           System.out.println("共找到"+sum+"组吸血鬼数"); 
  21.         }
  22. }

抱歉!评论已关闭.