这个是四位的吸血鬼数字计算,虽然有点罗嗦,不过挺好玩的。
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;
}
}
}
}
}
}
|