百度固然方便,还是自己动手动脑一下,比较好吧,作者: 史卜坤 时间: 2012-6-12 21:09
这个是四位的吸血鬼数字计算,虽然有点罗嗦,不过挺好玩的。
public class Xixueguinumber
{
public int cal(int x, int y, int z, int w)
{
int num = (x * 10 + y) * (z * 10 + w);
return num;
}
public static void main(String[] args)
{
for (int i = 1000; i <= 9999;i++)
{
int a = (int)(i/1000);
int b = (int)((i - a*1000)/100);
int c = (int)((i-((int)(i/100))*100)/10);
int d = (int)(i - ((int)(i/10))*10);
Xixueguinumber Xi = new Xixueguinumber();
boolean b1 = Xi.cal(a,b,c,d) == i;
boolean b2 = Xi.cal(b,a,c,d) == i;
boolean b3 = Xi.cal(a,b,d,c) == i;
boolean b4 = Xi.cal(b,a,d,c) == i;
boolean b5 = Xi.cal(a,c,b,d) == i;
boolean b6 = Xi.cal(a,c,d,b) == i ;
boolean b7 = Xi.cal(c,a,b,d) == i;
boolean b8 = Xi.cal(c,a,d,b) == i;
boolean b9 = Xi.cal(a,d,b,c) == i;
boolean b10 = Xi.cal(d,a,b,c) == i;
boolean b11 = Xi.cal(a,d,c,b) == i;
boolean b12 = Xi.cal(d,a,c,b) == i;
Boolean array[] ={b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12};
for(int j = 0; j <12; j++)
{
if(array[j])
{
System.out.print("吸血鬼数字为:" + i + "=" );
switch(j+1)
{
case 1 : System.out.println(""+ a + b + " * " + c + d);break;
case 2 : System.out.println(""+ b + a + " * " + c + d);break;
case 3 : System.out.println(""+ a + b + " * " + d + c);break;
case 4 : System.out.println(""+ b + a + " * " + d + c);break;
case 5 : System.out.println(""+ a + c + " * " + b + d);break;
case 6 : System.out.println(""+ a + c + " * " + d + b);break;
case 7 : System.out.println(""+ c + a + " * " + b + d);break;
case 8 : System.out.println(""+ c + a + " * " + d + b);break;
case 9 : System.out.println(""+ a + d + " * " + b + c);break;
case 10 : System.out.println(""+ d + a + " * " + b + c);break;
case 11 : System.out.println(""+ a + d + " * " + c + b);break;
case 12 : System.out.println(""+ d + a + " * " + c + b);break;
}
}
}
}
}
} 作者: 淡然 时间: 2012-6-12 21:32
史卜坤 发表于 2012-6-12 21:09
这个是四位的吸血鬼数字计算,虽然有点罗嗦,不过挺好玩的。
public class Xixueguinumber
{
Good!~ 好直接的方法作者: 郭宁 时间: 2012-6-12 22:36
public class XiXueGui {
public static void main(String[] args){
int bits=1;// 例 A*B bits 代表 A的位数
int temp_i;
int[] b =new int[10];
// System.out.println(b[5]);
for(int i=0;i<1000;i++){
temp_i = i;
while(temp_i/10!=0){
temp_i/=10;
bits*=10;
}
// System.out.println(bits);
for(int j=bits; j<bits*10; j++){
setfalse(b); //标记数组全部置为 false
setture(b,i*j);// 将答案拆分成一位一位的 在对应的 标记为置 true;
if(equal(b,i,j))
System.out.println(i+" * "+j+" = "+i*j);
}
bits=1;
}
}
//检测是否相等
private static boolean equal(int[] b,int i, int j){