“吸血鬼”数字

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);
}


}




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个Java程序,可以找出所有4位数的吸血鬼数字: ```java public class VampireNumbers { public static void main(String[] args) { for (int i = 1000; i < 10000; i++) { int a = i / 1000; int b = (i / 100) % 10; int c = (i / 10) % 10; int d = i % 10; if (checkVampireNumber(a, b, c, d, a, b, c, d)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, a, b, d, c)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, a, c, b, d)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, a, c, d, b)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, a, d, b, c)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, a, d, c, b)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, b, a, c, d)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, b, a, d, c)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, b, c, a, d)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, b, c, d, a)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, b, d, a, c)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, b, d, c, a)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, c, a, b, d)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, c, a, d, b)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, c, b, a, d)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, c, b, d, a)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, c, d, a, b)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, c, d, b, a)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, d, a, b, c)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, d, a, c, b)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, d, b, a, c)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, d, b, c, a)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, d, c, a, b)) { System.out.println(i); } if (checkVampireNumber(a, b, c, d, d, c, b, a)) { System.out.println(i); } } } public static boolean checkVampireNumber(int a, int b, int c, int d, int w, int x, int y, int z) { int num1 = 10 * a + b; int num2 = 10 * c + d; int product = num1 * num2; int firstHalf = 10 * w + x; int secondHalf = 10 * y + z; return product == firstHalf * secondHalf && product % 100 != 0; } } ``` 该程序通过枚举所有可能的数字组合,并检查它们是否满足吸血鬼数字的定义来找出所有4位数的吸血鬼数字
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cacasi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值