public class xixuegui {
    public static void main (String[] args) {
       int i,j,k,a,b,c,d,n,m;
       for(i=1001;i<9999;i++){
       a=i/1000;
       b=(i-a*1000)/100;
       c=(i-a*1000-b*100)/10;
       d=i-a*1000-b*100-c*10; 
      
       for(j=1;j<10;j++){
           for(k=0;k<10;k++){
               if(i==(j*1000)+(k*100)) continue;
           }
       }
         
       if(i==(a*10+b)*(c*10+d)) System.out.println(i+"is xixuegui");
       if(i==(a*10+b)*(d*10+c)) System.out.println(i+"is xixuegui");
       if(i==(b*10+a)*(c*10+d)) System.out.println(i+"is xixuegui");
       if(i==(b*10+a)*(d*10+c)) System.out.println(i+"is xixuegui");
         
       if(i==(a*10+c)*(b*10+d)) System.out.println(i+"is xixuegui");
       if(i==(a*10+c)*(d*10+b)) System.out.println(i+"is xixuegui");
       if(i==(c*10+a)*(b*10+d)) System.out.println(i+"is xixuegui");
       if(i==(c*10+a)*(d*10+b)) System.out.println(i+"is xixuegui");
         
       if(i==(a*10+d)*(b*10+c)) System.out.println(i+"is xixuegui");
       if(i==(a*10+d)*(c*10+b)) System.out.println(i+"is xixuegui");
       if(i==(d*10+a)*(b*10+c)) System.out.println(i+"is xixuegui");
       if(i==(d*10+a)*(c*10+b)) System.out.println(i+"is xixuegui");
         
       if(i==(b*10+c)*(a*10+d)) System.out.println(i+"is xixuegui");
       if(i==(b*10+c)*(d*10+a)) System.out.println(i+"is xixuegui");
       if(i==(c*10+b)*(a*10+d)) System.out.println(i+"is xixuegui");
       if(i==(c*10+b)*(d*10+a)) System.out.println(i+"is xixuegui");
     }
  }
}求助各位啊~~~这是我刚写的一个输出4位数"吸血鬼数字"的程序,虽然可以正确输出,但是我觉着这个做法太麻烦了,想得到一个简化代码的而且高效的方法,而且我这程序实际可以说还没完成,因为输出有重复,而我不希望有重复,虽然我知道一个方法,可是由于代码太多我实在觉得那样写出来有点丢人,所以求助各位帮帮我啦,小弟感激不尽~~~~~~差点忘了,所谓“吸血鬼数字”就是指位数为偶数的数字(我们算得是4位的),可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数字,其中从偶数位数字中选取的数字可以任意排列。以两个0截尾的数字是不允许的,例如:1260=21*601827=21*872187=27*81等等。大家帮帮忙啊~~~~~小弟感激不尽啊~~~

解决方案 »

  1.   

    java编程思想上的题吧,我也做了,我是先把提取的4个数全排列,然后一个一个测试的。而不是像你这样全列出来,而且4个数字全排列应该有24种,你咋只有16种呢
      

  2.   

    public static void main(String[] arg){  
        String[] ar_str1,ar_str2;  
        int sum=0;  
        //双重循环穷举  
        for(int i=10;i<100;i++){   
          //j=i+1避免重复   
          for(int j=i+1;j<100;j++){    
            int i_val=i*j;    
            if(i_val<1000||i_val>9999)
              continue; //积小于1000或大于9999排除,继续下一轮环    
            ar_str1=String.valueOf(i_val).split("");    
            ar_str2=(String.valueOf(i)+String.valueOf(j)).split("");    
            java.util.Arrays.sort(ar_str1);    
            java.util.Arrays.sort(ar_str2);    
            if(java.util.Arrays.equals(ar_str1, ar_str2)){
              //排序后比较,为真则找到一组      
              sum++;     
              System.out.println("第"+sum+"组: "+i+"*"+j+"="+i_val);    
            }   
          }  
        }  
        System.out.println("共找到"+sum+"组吸血鬼数"); 
      }第1组: 15*93=1395
    第2组: 21*60=1260
    第3组: 21*87=1827
    第4组: 27*81=2187
    第5组: 30*51=1530
    第6组: 35*41=1435
    第7组: 80*86=6880
    共找到7组吸血鬼数
      

  3.   

    关于吸血鬼数:
          若 合成数 v 有偶数个位n ,且是 n/2 个位的正整数 x 和 y 的积,而且 x 和 y 不是同时以0为个位数,组成 x 和 y 的数字,刚好就是 v 的数字,那麽 v 就是 吸血鬼数 (vampire number),而 x 和 y 则称为 尖牙 。      例如1260是吸血鬼数,21和60是其尖牙,因为21×60=1260。可是126000=210×600却非,因为210和600都以0为个位数。 吸血鬼数是 傅利曼数 的一种。 1994年 柯利弗德·皮寇弗 在 Usenet 社群sci.math的文章中首度提出吸血鬼数。後来皮寇弗将吸血鬼数写入他的书 Keys to Infinity 的第30章。
      

  4.   

    sureyor 的方法很好,排序后再比较,算法不错。
      

  5.   

    public class VampireNumber {
    private int isVampire(int a, int b) {
    int[] number = new int[4];
    number[0] = a / 10;
    number[1] = a % 10;
    number[2] = b / 10;
    number[3] = b % 10;
    int c = a * b;
    int[] number1 = new int[4];
    for (int i = 0, temp = c, div = 1000; i < number1.length; i++, div /= 10) {
    number1[i] = temp / div;
    temp = temp % div;
    }
    if (compare(number, number1))
    return c;
    else
    return -1;
    } private boolean compare(int[] number, int[] number1) {
    number = sort(number);
    number1 = sort(number1);
    for (int i = 0; i < number.length; i++)
    if (number[i] != number1[i])
    return false;
    return true;
    } private int[] sort(int[] number) {
    for (int i = 0; i < number.length - 1; i++)
    for (int j = i; j >= 0; j--) {
    if (number[j] > number[j + 1]) {
    int temp = number[j];
    number[j] = number[j + 1];
    number[j + 1] = temp;
    }
    }
    return number;
    } public static void main(String[] args) {
    VampireNumber v = new VampireNumber();
    for (int a = 10; a < 100; a++) {
    for (int b = a; b < 100; b++) {
    int c = v.isVampire(a, b);
    if (c > 0)
    System.out.println("a = " + a + ", b= " + b + ", c = " + c);
    }
    }
    }
    }
    a = 15, b= 93, c = 1395
    a = 21, b= 60, c = 1260
    a = 21, b= 87, c = 1827
    a = 27, b= 81, c = 2187
    a = 30, b= 51, c = 1530
    a = 35, b= 41, c = 1435
    a = 80, b= 86, c = 6880
      

  6.   

    一个很简单的问题,怎么搞得这么复杂
    这只是两个循环就能解决问题了
    哪有什么必要什么全排列啊
    解决问题算法最重要啊
    不要一味只看到题目就动手做
    等思路想好了,你就会发现会事半功倍的public class xixuegui {
      public static void main(String[] g) {    int num1, num2, product;
        int[] startDigit = new int[4];
        int[] productDigit = new int[4];
        int count = 0;
        int vampCount = 0;
        int x, y;
        for(num1 = 10; num1 <= 99; num1++)
          for(num2 = 10; num2 <= 99; num2++) {
            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;
            count = 0;
            for(x = 0; x < 4; x++)
              for(y = 0; y < 4; y++) {
                if (productDigit[x] == startDigit[y]){
                  count++;
                  productDigit[x] = -1;
                  startDigit[y] = -2;
                  if (count == 4) {
                    vampCount++;
                    int a = (int)Math.random() * 100;
                    int b = (int)Math.random() * 100;
                    int c = (int)Math.random() * 100;
                    if (vampCount < 10) {
                      System.out.println("Vampire number   "
                        + vampCount + " is " + num1 +
                        " * " + num2 + " = "+ product);
                    } else {
                       System.out.println("Vampire number  "
                        + vampCount + " is " + num1 +
                        " * " + num2 + " = "+ product);
                    }
                  }
                }
              }
          }
      }
    }
      

  7.   

    我是这样写的
    package base;public class Bloodsucker { static int rowNum = 1;

    public static void main(String[] args) {
    for(int i = 20; i <= 99;i++){
    for(int y = 50; y <= 99;y++){
    if(y < i)
    continue;

    //get sucked's number
    int check = i * y;
    int idown = i%10;
    int iup = i/10;
    int ydown = y%10;
    int yup = y/10;
    //get sucker's number
    int checkdown = check%10;
    int checkmindown = (check/10)%10;
    int checkminup = (check/100)%10;
    int checkup = check/1000;

    int[] sucked = {idown,iup,ydown,yup};
    int[] sucker = {checkdown,checkmindown,checkminup,checkup};
    int[]  ={0,0,0,0};

    for(int j = 0;j < 4;j++){
    for(int k = 0;k < 4;k++){
    if([k] ==0){
    if(sucker[j] == sucked[k]){
    [k] = 1;
    break;
    }
    }
    }
    }
    if([0] == 1 && [1] == 1 && [2] == 1 && [3] == 1)
    System.out.println("row " + rowNum++ + ": the bloodsucker is " + check +
    ",it sucked " + i + " and " + y + ";");
    }
    } }
    }
    结果是:
    row 1: the bloodsucker is 1260,it sucked 21 and 60;
    row 2: the bloodsucker is 1827,it sucked 21 and 87;
    row 3: the bloodsucker is 2187,it sucked 27 and 81;
    row 4: the bloodsucker is 1530,it sucked 30 and 51;
    row 5: the bloodsucker is 6880,it sucked 80 and 86;
      

  8.   

    上面的取值范围写小了  改了一下package base;public class Bloodsucker { static int rowNum = 1;

    public static void main(String[] args) {
    for(int i = 10; i <= 99;i++){
    for(int y = 10; y <= 99;y++){
    if(y < i)
    continue;

    //get sucked's number
    int check = i * y;
    int idown = i%10;
    int iup = i/10;
    int ydown = y%10;
    int yup = y/10;
    //get sucker's number
    int checkdown = check%10;
    int checkmindown = (check/10)%10;
    int checkminup = (check/100)%10;
    int checkup = check/1000;

    int[] sucked = {idown,iup,ydown,yup};
    int[] sucker = {checkdown,checkmindown,checkminup,checkup};
    int[]  ={0,0,0,0};

    for(int j = 0;j < 4;j++){
    for(int k = 0;k < 4;k++){
    if([k] ==0){
    if(sucker[j] == sucked[k]){
    [k] = 1;
    break;
    }
    }
    }
    }
    if([0] == 1 && [1] == 1 && [2] == 1 && [3] == 1)
    System.out.println("row " + rowNum++ + ": the bloodsucker is " + check +
    ",it sucked " + i + " and " + y + ";");
    }
    } }
    }结果是:
    row 1: the bloodsucker is 1395,it sucked 15 and 93;
    row 2: the bloodsucker is 1260,it sucked 21 and 60;
    row 3: the bloodsucker is 1827,it sucked 21 and 87;
    row 4: the bloodsucker is 2187,it sucked 27 and 81;
    row 5: the bloodsucker is 1530,it sucked 30 and 51;
    row 6: the bloodsucker is 1435,it sucked 35 and 41;
    row 7: the bloodsucker is 6880,it sucked 80 and 86;
      

  9.   

    sureyor()  对包里提供的工具很熟悉啊
      

  10.   

    //: c03:E11_Vampire.java
    // Solution by Dan Forhan
    import java.applet.*;
    import java.awt.*;public class Vampire extends Applet {
      private int num1, num2, product;
      private int[] startDigit = new int[4];
      private int[] productDigit = new int[4];
      private int count = 0;
      private int vampCount = 0;
      private int x, y;
      public void paint(Graphics g) {
        g.drawString("Vampire Numbers", 10, 20);
        g.drawLine(10, 22, 150, 22);
        // Positioning for output to applet:
        int column = 10, row = 35;
        for(num1 = 10; num1 <= 99; num1++)
          for(num2 = 10; num2 <= 99; num2++) {
            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;
            count = 0;
            for(x = 0; x < 4; x++)
              for(y = 0; y < 4; y++) {
                if (productDigit[x] == startDigit[y]){
                  count++;
                  productDigit[x] = -1;
                  startDigit[y] = -2;
                  if (count == 4) {
                    vampCount++;
                    int a = (int)Math.random() * 100;
                    int b = (int)Math.random() * 100;
                    int c = (int)Math.random() * 100;
                    if (vampCount < 10) {
                      g.drawString("Vampire number   "
                        + vampCount + " is " + num1 +
                        " * " + num2 + " = "+ product,
                        column, row);
                      row += 20;
                    } else {
                      g.drawString("Vampire number  "
                        + vampCount + " is " + num1 +
                        " * " + num2 + " = "+ product,
                        column, row);
                      row += 20;
                    }
                  }
                }
              }
          }
      }
    } ///:~
    这个是Thinking in Java作者给的答案,我在《Thinking in Java, 2nd edition, Annotated Solution Guide Revision 1.0》里找到的。
      

  11.   

    void fun()
    {
    int b[4]={0,0,0,0};
    int c[3]={0,0,0};
    int d[2]={0,0};
    int e[1]={0};
    int f[4]={0,0,0,0};
    for(int i=1000;i<9999;i++)
    {
    //取出千百十个位
    b[3]=i/1000;
    b[2]=(i/100)%10;
    b[1]=i%100/10;
    b[0]=i%10;
    for(int j=0;j<4;j++)
    {
    int temp0=(b[2])*(b[0]);
    if(temp0%10!=b[j]) continue;
    if(temp0%10==b[j])//比较个位
    {
    c[2]=b[(j+1)%4];
    c[1]=b[(j+2)%4];
    c[0]=b[(j+3)%4];
    f[0]=b[j];

    for(int k=0;k<3;k++)
    {
    int temp1=temp0/10+b[1]*b[2]%10+b[0]*b[3]%10;
    if(temp1%10!=c[k])continue;
    if(temp1%10==c[k])//比较十位
    {
    d[0]=c[(k+1)%3];
    d[1]=c[(k+2)%3];
    f[1]=c[k];
    for(int l=0;l<2;l++)
    {
    int temp2=b[0]*b[3]/10+b[1]*b[2]/10+b[3]*b[1]%10;
    if(temp1/10+temp2%10!=d[l])continue;
    if(temp1/10+temp2%10==d[l])//比较百位
    {
    e[0]=d[(l+1)%2];
    f[2]=d[l];
    if((temp2/10+b[1]*b[3]/10==e[0])&&e[0])//比较千位
    {
    f[3]=e[0];
    for(int m=3;m>=0;m--)
    {

    cout<<f[m];
    }
    cout<<endl;
    }
    }

    }
    }
    }

    }
    }


    }

    }
      

  12.   

    import java.util.Arrays;public class Xi {
    public static void main(String[] args) {
    for(int i=1;i<100;i++){
    for(int j=1;j<100;j++){
    if(Xi.isXiXueGui(i, j)){
    System.out.println(i+"*"+j+"="+i*j);
    }
    }
    }
    }

    public static boolean isXiXueGui(int x1, int x2) {
    String strx1 = Integer.toString(x1);
    String strx2 = Integer.toString(x2);
    String x1x2 = strx1 + strx2;
    String xxg = Integer.toString(x1 * x2); int x1Len = strx1.length();
    int x2Len = strx2.length();
    int xxgLen = xxg.length(); if ((strx1.indexOf(strx1.length() - 1) != '0' && strx2.indexOf(strx2
    .length() - 1) != '0')
    && x1Len == x2Len && x1Len + x2Len == xxgLen) {
    } else {
    return false;
    } char[] x1x2c = x1x2.toCharArray();
    Arrays.sort(x1x2c);
    char[] xxgc = xxg.toCharArray();
    Arrays.sort(xxgc); x1x2 = new String(x1x2c);
    xxg = new String(xxgc);
    if (x1x2.equals(xxg)) {
    return true;
    }
    return false; }
    }
      

  13.   

    import java.util.Arrays;public class Test { public static void main(String[] args) {

    int sum = 0;

    for(int i = 10; i < 99; i++){
    for(int j = i + 1; j < 99; j++){

    int product = i * j;
    if(product > 9999 || product < 1000 || product % 100 == 0) continue;

    String strProduct = "" + product;
    String gene = "" + i + j;

    String[] arrProduct = strProduct.split("");
    String[] arrGene = gene.split("");

    Arrays.sort(arrProduct);
    Arrays.sort(arrGene);

    if(Arrays.equals(arrProduct, arrGene)){
    sum++;
    System.out.println("第" + sum + "组: " + strProduct);
    }
    }
    }
    }
    }
      

  14.   

    重点在于数论中的 ((product  - i - j ) %9 == 0 )if(product > 9999 || product < 1000 || product % 100 == 0) continue;
    if(product > 9999 || product < 1000 || product % 100 == 0  || ((product  - i - j )%9 != 0 )
    ) continue;重中之重,影响效率的关键因素
      

  15.   

     在每一句if(i==(a*10+b)*(c*10+d))   System.out.println(i+ "is   xixuegui "); 
    后面加上一个continue;就不会重复了,我有试过
      

  16.   

    class DrinkGhost{
    public static void main(String[] args) {
    for(int i =1001;i<10000;i++){
    Integer p = i;
    String[] s =p.toString().split("");
    for(int j = 2;j<5;j++){
    int a =Integer.parseInt(s[1]);
    int a1 =Integer.parseInt(s[j]);
    int A=a*10+a1;
    int A1=a1*10+a;
    double q = (double)j;
    double h=Math.ceil((9-q)/2-1);
    int w = (int)h;
    int b =Integer.parseInt(s[w]);
    int b1=Integer.parseInt(s[10-1-j-w]);
    int B=b*10+b1;
    int B1=b1*10+b;
    int result = A*B;
    int result1 = A*B1;
    int result2 = A1*B;
    int result3 = A1*B1;
    if(i==result){
    System.out.println(i+"="+A+"*"+B);
    break;
    }else if(i==result1){
    System.out.println(i+"="+A+"*"+B1);
    break;
    }else if(i==result2){
    System.out.println(i+"="+A1+"*"+B);
    break;
    }else if(i==result3){
    System.out.println(i+"="+A1+"*"+B1);
    break;
    }else{
    continue;
    }

    }
    //System.out.println(s[0]);

    }

    }
    }
    结果运行是:::
     F:\xiao\java\excise>javac DrinkGhost.javaF:\xiao\java\excise>java DrinkGhost
    1260=21*60
    1395=15*93
    1435=41*35
    1530=51*30
    1827=21*87
    2187=27*81
    6880=86*80
      

  17.   

    楼主的for(j=1;j <10;j++){ 
                          for(k=0;k <10;k++){ 
                                  if(i==(j*1000)+(k*100))   continue; 
                          } 
                  } 
    这段属于废材,continue根本连二层循环都没跳出,根本不用说判断了,还有如果硬要用组合的话不会有16种,只有12种。楼主所写的里面有4组重复的。
    使用双循环上面各位大神都写了好多。。不过里面用到String里面的好多函数,在做think in java时根本没见过,所以我只能用一个循环判断来做了各位大神也指教指教:
    将两位数的各位写在a[4]里,四位数的写在b[4]里:
    int k=0;
    for(int i=0;i<4;i++){
        for(int j=0;i<4;j++){
            if(a[i]==b[j]){
                b[j]=-1;
                k++;
            }
        }
    }
    if (k==4)
     {……}//打印吸血数
    个人感觉刚开始的那个判断没有必要有 product   >   9999   的这个判断,毕竟两位数相乘不会比这个打,至于上面有个大神写的((product     -   i   -   j   )   %9   ==   0   ) 这句,确实很强大,但是没弄明白为什么
      

  18.   

    上面的忘记在k++;后面加句break;失误啊!
      

  19.   

    凑凑热闹,刚写的
    class VampireDigital
    {
    private int m1,m2,m3,m4;
    private int k1,k2,k3,k4;
    private int number;

    VampireDigital()
    {
    create();
    }

    public void create()
    {
    for(number = 1000; number < 10000; number++)
    {
    m1 = number / 1000;
    m2 = number % 1000 / 100;
    m3 = number % 100 / 10;
    m4 = number % 10;

    if(m3 == m4)
    {
    continue;
    }

    calculate(m1, m2, m3, m4);
    count();
    calculate(m1, m3, m2, m4);
    count();
    calculate(m1, m4, m2, m3);
    count();
    }
    }



    public void count()
    {
      if((k1 * k3 == number) || (k1 * k4 == number) || (k2 * k3 == number) || (k2 * k4 == number))
      {
      System.out.print(number + " ");
      }
    }

    public void calculate(int n1, int n2, int n3,int n4)
    {
    k1 = n1 * 10 + n2;
    k2 = n2 * 10 + n1;
    k3 = n3 * 10 + n4;
    k4 = n4 * 10 + n3;
    }}
    public class Test
    {
    public static void main (String[] args) 
    {
    VampireDigital vd = new VampireDigital();

    }
    }
      

  20.   

    第一次写的  继续改进学习
    public class Xixuegui { public static void main(String[] args) {
    for(int i = 10;i<100;i++)
    {
    for(int j = 10;j<100;j++)
    {
    int num;
    int a,b,c,d;
    a = i/10; b = i%10;
    c = j/10; d = j%10;
    num = a*1000+b*100+c*10+d;
    if(num == i*j || num == i*((d*10)+c) || num ==(a*10+c)*(b*10+d) 
    || num == (a*10+c)*(d*10+b) || num==(a*10+d)*(b*10+c)
    || num==(a*10+d)*(c*10+b)

    || num==(b*10+a)*(c*10+d)|| num==(b*10+a)*(d*10+c) 
    || num==(b*10+c)*(a*10+d)|| num==(b*10+c)*(d*10+a) 
    || num==(b*10+d)*(a*10+c)|| num==(b*10+d)*(c*10+a)

    || num==(c*10+a)*(b*10+d)|| num==(c*10+a)*(d*10+b) 
    || num==(c*10+b)*(a*10+d)|| num==(c*10+b)*(d*10+a) 
    || num==(c*10+d)*(a*10+b)|| num==(c*10+d)*(b*10+a)

    || num==(d*10+a)*(b*10+c)|| num==(d*10+a)*(c*10+b) 
    || num==(d*10+b)*(a*10+c)|| num==(d*10+b)*(c*10+a) 
    || num==(d*10+c)*(a*10+b)|| num==(d*10+c)*(b*10+a))

    System.out.println("The number is :" + num);
    else
    continue;
    }
    }
    }
    }
      

  21.   

    7楼的是自己写的吗? 其中的a,b,c是干嘛用的?