日期:2014-05-20  浏览次数:20654 次

关于吸血鬼数字
A vampire number has an even number of digits and is formed by multiplying a pair of numbers containing half the number of digits of the result. The digits are taken from the original number in any order. Pairs of trailing zeroes are not allowed. Examples include:
1260 = 21 * 60
1827 = 21 * 87
2187 = 27 * 81
Write a program that finds all the 4-digit vampire numbers. (Suggested by Dan Forhan.)

Java code
import java.util.*;
public class Vampire {
    public static void main(String [] args){
        int [] a = new int [4];   //数组a中存放四位数的每位数字
        int [][] b = new int [4][3];  //存放的四个数字可以12个两位数,以二维数组形式存放
        for(int i = 1000; i <10000; i++){
            int count = 0;   //count 用于判断条件
            int nu = i;   //临时变量,代替当前i值
            for(int j = 0 ; j < 4; j++){   //取出四位数中的每位数字,存放在数组a中
                a[j] = nu % 10;   //取余,即取末尾数字
                nu = nu / 10;     //nu迭代,略去最后一位数字
            }
            //将取出来的四位数字,组合成两位数,存放在二位数组中
            //比如四位数字a,b,c,d,数组{a,b,c,d},组成的12个两位数是
            // ab,ba,ca,da
            // ac,bc,cb,db
            // ad,bd,cd,dc
            for(int j = 0; j < 4; j++){
                int m = 0;
                for(int k = 0; k < 3 ; k++){
                        if(m == j) //每个数字不能同自己组合,比如没有aa,bb,cc,dd
                            m++;   //碰到自身,m++,跳过自己与自己组合的可能性
                        if( m==4 ) //当 m=j=3时,即到数字d时,通过上一步m++变为4,这时m已经越界了
                            break; //用break语句跳出循环,防止越界,因为 a[4] 不存在
                        b[j][k] = a[j]*10 + a[m];
                        m++; // m++,b[0][3]跳到b[1][0],b[1][3]跳到b[2][0],b[2][3]跳到b[3][0]
                    }
                }
            //鉴于二维数组的操作性,将二维数组里的内容赋到一维数组中
            int n = 0;
            int [] c = new int [12]; //实例一维数组 c[12]
                for(int k = 0; k < 4; k++){
                    for(int l = 0 ; l < 3; l++){
                        c[n] = b[k][l]; //将二维数组内容逐个赋给一维数组
                        n++;
                    }
                }
            Arrays.sort(c); //将数组排序,升序
            Arrays.sort(a); //将数组排序,升序
            //判别吸血鬼数字
            for(int j = 1 ; j < 12 ;j++){  //c[j] 从第二项开始
                for(int k = 0; k < j ; k++){ //c[k] 从第一项开始,避免重复,k<j
                    if( i == c[j]*c[k]){   //第一个满足条件,i==c[j]*c[k]
                        int cj = c[j];    //临时变量cj 代替c[j]
                        int ck = c[k];    //临时变量ck 代替c[k]
                        int [] aa = new int [4]; //实例数组aa,用于存放c[j],c[k]包含的
                                                 // 四个数字,用于同数组a比较
                        aa[0] = cj % 10; //取c[j]个位数字
                        cj = cj / 10;    //
                        aa[1] = cj;      //取c[j]十位数字                    
                        aa[2] = ck % 10; //取c[k]个位数字
                        ck = ck /10;     //
                        aa[3] = ck;      //取c[k]十位数字 
                        Arrays.sort(aa); //对数组aa排序,升序,便于同数组a比较
                        if(Arrays.equals(a, aa)){  // 比较数组a同aa的内容是否相同
                            if(!(c[j] % 10 == 0 && c[k] % 10 == 0)){ //剔除两个数都是以0结尾的
                                count++; //若找到count++
                                System.out.println("vampire number : " + i +
                                        "\nmultiplying by : " + c[j] + " and " + c[k]);
                            }
                        }
                    }
                }
            }
            if(count > 0)
                System.out.println();
        }
    }
}



运行结果郁闷

run:
vampire number : 1260
multiplying by : 60 and 21

vampire number : 1395
multiplying by : 93 and 15

vampire number : 1435
multiplying by : 41 and 35

vampire number : 1530
multiplying by : 51 and 30

vampire number : 1827
multiplying by : 87 and 21

vampire number : 2187