“吸血鬼”数字

来源:互联网 发布:轩辕剑 知乎 编辑:程序博客网 时间:2024/04/27 19:30
import java.util.*;
import java.io.*;




/* by gyq date'2013-06-02'
 * 
 * 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数
 * 的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,
 * 下列数字都是 “吸血鬼”数字;
 * 
 * 1260 = 21 * 60
 * 1827 = 21 * 87
 * 2187 = 27 * 81
 * 
 * 以下数字为打出四位数的所有“吸血鬼”数字.
 * 
 */


public class javaTestTwo {


/**
* @param args
*/

static void print_all_group_of_a_num(int num)
{
if(num == 0 || num == 1)
{
System.out.println("The num is " + num + "!");
return;
}
else if(num > 1)
{
System.out.println("The num is " + num + "!");

long square_num = (long)Math.round(Math.sqrt(num));
System.out.println("The square num is " + square_num);//要求的数的开方

for(int i=2; i<=square_num; i++)
{
if(num % i == 0)
{
System.out.println("The num is " + i + "*" + num/i + "." );
}
}



}

return;
}

static void print_all_vampire_num(long num)
{
if(num % 100 == 0)
return;

if(num>=1000 && num<=9999)
{
long a = num / 1000;
long b = (num - a*1000)/100;
long c = num%100/10;
long d = num%10;

long[] numAry = new long[4];
numAry[0] = a;
numAry[1] = b;
numAry[2] = c;
numAry[3] = d;

for(int i=0; i<=3; i++)
{
for(int j=0; j<=3; j++)
{
if(i != j)
{

if(numAry[i] == 0)
continue;

/* System.out.print(numAry[i]);
System.out.print(" ");
System.out.print(numAry[j]);
System.out.print(",");*/

long[] tmpAry = new long[2];
int tmp = 0;
for(int k=0; k<=3; k++)
{
if(k!=i && k!=j)
{
tmpAry[tmp++] = numAry[k]; 
}

}
if(tmpAry[0] == 0)
continue;
/* System.out.print(tmpAry[0]);
System.out.print(" ");
System.out.print(tmpAry[1]);*/

long numOne = numAry[i] * 10 + numAry[j];
long numtwo = tmpAry[0] * 10 + tmpAry[1];
/*System.out.print(" ");
System.out.print(numOne);
System.out.print(",");
System.out.print(numtwo);*/

if(numOne * numtwo == num)
{
System.out.print(numOne);
System.out.print("*");
System.out.print(numtwo);
System.out.print("=");
System.out.print(num);
System.out.print(" It's vampire!");
System.out.println("");
}





}

}
}

}

return;
}

public static void main(String[] args) {
// TODO Auto-generated method stub


//print_all_group_of_a_num(1260);


for(int i = 1000; i<10000; i++ )
{
print_all_vampire_num(i);
}


}




}