的java代码
/*我编的小程序,逻辑有点混乱,注释也少得可怜,但应该能列出任何能用四则运算得出24的四个1~10的数的等于24的等式。
大家若有空,请指点。*/
public class Test24Point{
public static void main(String[] args){
int index = 0 ;
int temp = 0 ;
int totalSuc = 0 ;
int numb[] = new int[4];//the first four numbers
double num[][] = new double[36][3];//three numbers after calculating
double total[] = new double[6];//the number after three steps of calculating
double p[][] = new double[6][8];
double q[][] = new double[3][7];
//System.out.println(2465%108);
//System.out.println(2465/108);
System.out.println("\"a--b\"means\"b-a\"");
System.out.println("\"a//b\"means\"b/a\"\n");
/* for(int h = 0; h <= 9; h ++)//Get the first four numbers for calculating and store into the array numb[4];.
for(int i = 0; i <= 9; i ++)
for(int j = 0; j <= 9; j ++)
for(int k = 0; k <= 9; k ++){
numb[0] = h ;
numb[1] = i ;
numb[2] = j ;
numb[3] = k ;
}*/
for(int i = 0 ; i < 4 ; i ++){
numb[i] = Integer.parseInt(args[i]);
}
for(int i = 0; i < 3; i ++)//Get two of the four to calculate and then store the new number into the array p;
for(int j = i + 1; j < 4 ; j ++,temp ++){
p[temp][0] = numb[i] + numb[j];
p[temp][1] = numb[i] - numb[j];
p[temp][2] = numb[j] - numb[i];
p[temp][3] = numb[i] * numb[j];
if(numb[j] != 0)
p[temp][4] = numb[i] / (double)numb[j];
else
p[temp][4] = 10000;
if(numb[i] != 0)
p[temp][5] = numb[j] / (double)numb[i];
else
p[temp][5] = 10000;
switch(temp){
case 0:p[temp][6] = numb[2]; p[temp][7] = numb[3];break;
case 1:p[temp][6] = numb[1]; p[temp][7] = numb[3];break;
case 2:p[temp][6] = numb[1]; p[temp][7] = numb[2];break;
case 3:p[temp][6] = numb[0]; p[temp][7] = numb[3];break;
case 4:p[temp][6] = numb[0]; p[temp][7] = numb[2];break;
case 5:p[temp][6] = numb[0]; p[temp][7] = numb[1];
}
}
for(int k = 0,tem = 0; k < 6; k ++)//Get the possible three numbers and store into the array num[36][3] for calculating .
for(int l = 0; l < 6; l ++,tem ++){
num[tem][0] = p[k][l] ;
num[tem][1] = p[k][6] ;
num[tem][2] = p[k][7] ;
for(int t = 2,m = 0, n = 0,te = 0; t >= 0; t --,te ++){//Get two of the three to calculate and then store the new number into the array q;
m = (t + 1)%3;
n = (t + 2)%3;
q[te][6] = num[tem][t];
q[te][0] = num[tem][m] + num[tem][n];
q[te][1] = num[tem][m] - num[tem][n];
q[te][2] = num[tem][n] - num[tem][m];
q[te][3] = num[tem][m] * num[tem][n];
if(num[tem][n] != 0)
q[te][4] = num[tem][m] / (double)num[tem][n];
else
q[te][4] = 10000 ;
if(num[tem][m] != 0)
q[te][5] = num[tem][n] / (double)num[tem][m];
else
q[te][5] = 10000 ;
}
for(int u = 0; u < 3; u ++)
for(int v = 0; v < 6; v ++){
if(u == 2){//We must insure that the old value is in the left ,so the result string can be appended rightly.
total[0] = q[u][6] + q[u][v];
total[1] = q[u][6] - q[u][v];
total[2] = q[u][v] - q[u][6];
total[3] = q[u][v] * q[u][6];
if(q[u][6] != 0)
total[4] = q[u][6] / (double)q[u][v];
else
total[4] = 10000;
if(q[u][v] != 0)
total[5] = q[u][v] / (double)q[u][6];
else
total[5] = 10000;
}
else{
total[0] = q[u][v] + q[u][6];
total[1] = q[u][v] - q[u][6];
total[2] = q[u][6] - q[u][v];
total[3] = q[u][v] * q[u][6];
if(q[u][6] != 0)
total[4] = q[u][v] / (double)q[u][6];
else
total[4] = 10000;
if(q[u][v] != 0)
total[5] = q[u][6] / (double)q[u][v];
else
total[5] = 10000;
}
for(int s = 0 ; s < 6 ; s ++){
if(total[s]>23.9999&&total[s]<24.0001){
//System.out.println("24!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
totalSuc ++ ;
//print the expression
char x[] = new char[3];
int n0 = index ;
String expre = "" ;
String expre1 = "" ;
int n1 = index/108;//the first composition and its operator.
int n2 = index%108/6;//the second composition and its operator.
int n3 = index%6;//the last operator.
//now ready to printout the equation.
switch(n1){
case 0:expre += "((a+b)";x[1]=c;x[2]=d;break;
case 1:expre += "((a-b)";x[1]=c;x[2]=d;break;
case 2:expre += "((b-a)";x[1]=c;x[2]=d;break;
case 3:expre += "((a*b)";x[1]=c;x[2]=d;break;
case 4:expre += "((a/b)";x[1]=c;x[2]=d;break;
case 5:expre += "((b/a)";x[1]=c;x[2]=d;break;
case 6:expre += "((a+c)";x[1]=b;x[2]=d;break;
case 7:expre += "((a-c)";x[1]=b;x[2]=d;break;
case 8:expre += "((c-a)";x[1]=b;x[2]=d;break;
case 9:expre += "((a*c)";x[1]=b;x[2]=d;break;
case 10:expre += "((a/c)";x[1]=b;x[2]=d;break;
case 11:expre += "((c/a)";x[1]=b;x[2]=d;break;
case 12:expre += "((a+d)";x[1]=b;x[2]=c;break;
case 13:expre += "((a-d)";x[1]=b;x[2]=c;break;
case 14:expre += "((d-a)";x[1]=b;x[2]=c;break;
case 15:expre += "((a*d)";x[1]=b;x[2]=c;break;
case 16:expre += "((a/d)";x[1]=b;x[2]=c;break;
case 17:expre += "((d/a)";x[1]=b;x[2]=c;break;
case 18:expre += "((b+c)";x[1]=a;x[2]=d;break;
case 19:expre += "((b-c)";x[1]=a;x[2]=d;break;
case 20:expre += "((c-b)";x[1]=a;x[2]=d;break;
case 21:expre += "((b*c)";x[1]=a;x[2]=d;break;
case 22:expre += "((b/c)";x[1]=a;x[2]=d;break;
case 23:expre += "((c/b)";x[1]=a;x[2]=d;break;
case 24:expre += "((b+d)";x[1]=a;x[2]=c;break;
case 25:expre += "((b-d)";x[1]=a;x[2]=c;break;
case 26:expre += "((d-b)";x[1]=a;x[2]=c;break;
case 27:expre += "((b*d)";x[1]=a;x[2]=c;break;
case 28:expre += "((b/d)";x[1]=a;x[2]=c;break;
case 29:expre += "((d/b)";x[1]=a;x[2]=c;break;
case 30:expre += "((c+d)";x[1]=a;x[2]=b;break;
case 31:expre += "((c-d)";x[1]=a;x[2]=b;break;
case 32:expre += "((d-c)";x[1]=a;x[2]=b;break;
case 33:expre += "((c*d)";x[1]=a;x[2]=b;break;
case 34:expre += "((c/d)";x[1]=a;x[2]=b;break;
case 35:expre += "((d/c)";x[1]=a;x[2]=b;
}
switch(n2){
case 0:expre += "+" +x[1] +")";x[1]=x[2];break;//x[0] and x[1].
case 1:expre += "-" +x[1] +")";x[1]=x[2];break;
case 2:expre += "--" +x[1] +")";x[1]=x[2];break;
case 3:expre += "*" +x[1] +")";x[1]=x[2];break;
case 4:expre += "/" +x[1] +")";x[1]=x[2];break;
case 5:expre += "//" +x[1] +")";x[1]=x[2];break;
case 6:expre += "+" +x[2] +")";x[1]=x[1];break;//x[2] and x[0].
case 7:expre += "--" +x[2] +")";x[1]=x[1];break;
case 8:expre += "-" +x[2] +")";x[1]=x[1];break;
case 9:expre += "*" +x[2] +")";x[1]=x[1];break;
case 10:expre += "//" +x[2] +")";x[1]=x[1];break;
case 11:expre += "/" +x[2] +")";x[1]=x[1];break;
case 12:expre1 += x[1] + "+" + x[2] +"))";x[1]=(;break;//x[1] and x[2].
case 13:expre1 += x[1] + "-" + x[2] +"))";x[1]=(;break;
case 14:expre1 += x[1] + "--" + x[2] +"))";x[1]=(;break;
case 15:expre1 += x[1] + "*" + x[2] +"))";x[1]=(;break;
case 16:expre1 += x[1] + "/" + x[2] +"))";x[1]=(;break;
case 17:expre1 += x[1] + "//" + x[2] +"))";x[1]=(;
}
switch(n3){
case 0:expre += "+" +x[1] + expre1;break;
case 1:expre += "-" +x[1] + expre1;break;
case 2:expre += "--" +x[1] + expre1;break;
case 3:expre += "*" +x[1] + expre1;break;
case 4:expre += "/" +x[1] + expre1;break;
case 5:expre += "//" +x[1] + expre1;
}
expre = expre.replace(a,(char)(numb[0] + 48));
expre = expre.replace(b,(char)(numb[1] + 48));
expre = expre.replace(c,(char)(numb[2] + 48));
expre = expre.replace(d,(char)(numb[3] + 48));
System.out.println(expre+" ");
}
//System.out.println("total : " + total[s] + " index :" +"\n\n");
index ++ ;
}
}
}System.out.println("The number of total successful expressions is : " + totalSuc);
}
}
/*我编的小程序,逻辑有点混乱,注释也少得可怜,但应该能列出任何能用四则运算得出24的四个1~10的数的等于24的等式。
大家若有空,请指点。*/
public class Test24Point{
public static void main(String[] args){
int index = 0 ;
int temp = 0 ;
int totalSuc = 0 ;
int numb[] = new int[4];//the first four numbers
double num[][] = new double[36][3];//three numbers after calculating
double total[] = new double[6];//the number after three steps of calculating
double p[][] = new double[6][8];
double q[][] = new double[3][7];
//System.out.println(2465%108);
//System.out.println(2465/108);
System.out.println("\"a--b\"means\"b-a\"");
System.out.println("\"a//b\"means\"b/a\"\n");
/* for(int h = 0; h <= 9; h ++)//Get the first four numbers for calculating and store into the array numb[4];.
for(int i = 0; i <= 9; i ++)
for(int j = 0; j <= 9; j ++)
for(int k = 0; k <= 9; k ++){
numb[0] = h ;
numb[1] = i ;
numb[2] = j ;
numb[3] = k ;
}*/
for(int i = 0 ; i < 4 ; i ++){
numb[i] = Integer.parseInt(args[i]);
}
for(int i = 0; i < 3; i ++)//Get two of the four to calculate and then store the new number into the array p;
for(int j = i + 1; j < 4 ; j ++,temp ++){
p[temp][0] = numb[i] + numb[j];
p[temp][1] = numb[i] - numb[j];
p[temp][2] = numb[j] - numb[i];
p[temp][3] = numb[i] * numb[j];
if(numb[j] != 0)
p[temp][4] = numb[i] / (double)numb[j];
else
p[temp][4] = 10000;
if(numb[i] != 0)
p[temp][5] = numb[j] / (double)numb[i];
else
p[temp][5] = 10000;
switch(temp){
case 0:p[temp][6] = numb[2]; p[temp][7] = numb[3];break;
case 1:p[temp][6] = numb[1]; p[temp][7] = numb[3];break;
case 2:p[temp][6] = numb[1]; p[temp][7] = numb[2];break;
case 3:p[temp][6] = numb[0]; p[temp][7] = numb[3];break;
case 4:p[temp][6] = numb[0]; p[temp][7] = numb[2];break;
case 5:p[temp][6] = numb[0]; p[temp][7] = numb[1];
}
}
for(int k = 0,tem = 0; k < 6; k ++)//Get the possible three numbers and store into the array num[36][3] for calculating .
for(int l = 0; l < 6; l ++,tem ++){
num[tem][0] = p[k][l] ;
num[tem][1] = p[k][6] ;
num[tem][2] = p[k][7] ;
for(int t = 2,m = 0, n = 0,te = 0; t >= 0; t --,te ++){//Get two of the three to calculate and then store the new number into the array q;
m = (t + 1)%3;
n = (t + 2)%3;
q[te][6] = num[tem][t];
q[te][0] = num[tem][m] + num[tem][n];
q[te][1] = num[tem][m] - num[tem][n];
q[te][2] = num[tem][n] - num[tem][m];
q[te][3] = num[tem][m] * num[tem][n];
if(num[tem][n] != 0)
q[te][4] = num[tem][m] / (double)num[tem][n];
else
q[te][4] = 10000 ;
if(num[tem][m] != 0)
q[te][5] = num[tem][n] / (double)num[tem][m];
else
q[te][5] = 10000 ;
}
for(int u = 0; u < 3; u ++)
for(int v = 0; v < 6; v ++){
if(u == 2){//We must insure that the old value is in the left ,so the result string can be appended rightly.
total[0] = q[u][6] + q[u][v];
total[1] = q[u][6] - q[u][v];
total[2] = q[u][v] - q[u][6];
total[3] = q[u][v] * q[u][6];
if(q[u][6] != 0)
total[4] = q[u][6] / (double)q[u][v];
else
total[4] = 10000;
if(q[u][v] != 0)
total[5] = q[u][v] / (double)q[u][6];
else
total[5] = 10000;
}
else{
total[0] = q[u][v] + q[u][6];
total[1] = q[u][v] - q[u][6];
total[2] = q[u][6] - q[u][v];
total[3] = q[u][v] * q[u][6];
if(q[u][6] != 0)
total[4] = q[u][v] / (double)q[u][6];
else
total[4] = 10000;
if(q[u][v] != 0)
total[5] = q[u][6] / (double)q[u][v];
else
total[5] = 10000;
}
for(int s = 0 ; s < 6 ; s ++){
if(total[s]>23.9999&&total[s]<24.0001){
//System.out.println("24!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
totalSuc ++ ;
//print the expression
char x[] = new char[3];
int n0 = index ;
String expre = "" ;
String expre1 = "" ;
int n1 = index/108;//the first composition and its operator.
int n2 = index%108/6;//the second composition and its operator.
int n3 = index%6;//the last operator.
//now ready to printout the equation.
switch(n1){
case 0:expre += "((a+b)";x[1]=c;x[2]=d;break;
case 1:expre += "((a-b)";x[1]=c;x[2]=d;break;
case 2:expre += "((b-a)";x[1]=c;x[2]=d;break;
case 3:expre += "((a*b)";x[1]=c;x[2]=d;break;
case 4:expre += "((a/b)";x[1]=c;x[2]=d;break;
case 5:expre += "((b/a)";x[1]=c;x[2]=d;break;
case 6:expre += "((a+c)";x[1]=b;x[2]=d;break;
case 7:expre += "((a-c)";x[1]=b;x[2]=d;break;
case 8:expre += "((c-a)";x[1]=b;x[2]=d;break;
case 9:expre += "((a*c)";x[1]=b;x[2]=d;break;
case 10:expre += "((a/c)";x[1]=b;x[2]=d;break;
case 11:expre += "((c/a)";x[1]=b;x[2]=d;break;
case 12:expre += "((a+d)";x[1]=b;x[2]=c;break;
case 13:expre += "((a-d)";x[1]=b;x[2]=c;break;
case 14:expre += "((d-a)";x[1]=b;x[2]=c;break;
case 15:expre += "((a*d)";x[1]=b;x[2]=c;break;
case 16:expre += "((a/d)";x[1]=b;x[2]=c;break;
case 17:expre += "((d/a)";x[1]=b;x[2]=c;break;
case 18:expre += "((b+c)";x[1]=a;x[2]=d;break;
case 19:expre += "((b-c)";x[1]=a;x[2]=d;break;
case 20:expre += "((c-b)";x[1]=a;x[2]=d;break;
case 21:expre += "((b*c)";x[1]=a;x[2]=d;break;
case 22:expre += "((b/c)";x[1]=a;x[2]=d;break;
case 23:expre += "((c/b)";x[1]=a;x[2]=d;break;
case 24:expre += "((b+d)";x[1]=a;x[2]=c;break;
case 25:expre += "((b-d)";x[1]=a;x[2]=c;break;
case 26:expre += "((d-b)";x[1]=a;x[2]=c;break;
case 27:expre += "((b*d)";x[1]=a;x[2]=c;break;
case 28:expre += "((b/d)";x[1]=a;x[2]=c;break;
case 29:expre += "((d/b)";x[1]=a;x[2]=c;break;
case 30:expre += "((c+d)";x[1]=a;x[2]=b;break;
case 31:expre += "((c-d)";x[1]=a;x[2]=b;break;
case 32:expre += "((d-c)";x[1]=a;x[2]=b;break;
case 33:expre += "((c*d)";x[1]=a;x[2]=b;break;
case 34:expre += "((c/d)";x[1]=a;x[2]=b;break;
case 35:expre += "((d/c)";x[1]=a;x[2]=b;
}
switch(n2){
case 0:expre += "+" +x[1] +")";x[1]=x[2];break;//x[0] and x[1].
case 1:expre += "-" +x[1] +")";x[1]=x[2];break;
case 2:expre += "--" +x[1] +")";x[1]=x[2];break;
case 3:expre += "*" +x[1] +")";x[1]=x[2];break;
case 4:expre += "/" +x[1] +")";x[1]=x[2];break;
case 5:expre += "//" +x[1] +")";x[1]=x[2];break;
case 6:expre += "+" +x[2] +")";x[1]=x[1];break;//x[2] and x[0].
case 7:expre += "--" +x[2] +")";x[1]=x[1];break;
case 8:expre += "-" +x[2] +")";x[1]=x[1];break;
case 9:expre += "*" +x[2] +")";x[1]=x[1];break;
case 10:expre += "//" +x[2] +")";x[1]=x[1];break;
case 11:expre += "/" +x[2] +")";x[1]=x[1];break;
case 12:expre1 += x[1] + "+" + x[2] +"))";x[1]=(;break;//x[1] and x[2].
case 13:expre1 += x[1] + "-" + x[2] +"))";x[1]=(;break;
case 14:expre1 += x[1] + "--" + x[2] +"))";x[1]=(;break;
case 15:expre1 += x[1] + "*" + x[2] +"))";x[1]=(;break;
case 16:expre1 += x[1] + "/" + x[2] +"))";x[1]=(;break;
case 17:expre1 += x[1] + "//" + x[2] +"))";x[1]=(;
}
switch(n3){
case 0:expre += "+" +x[1] + expre1;break;
case 1:expre += "-" +x[1] + expre1;break;
case 2:expre += "--" +x[1] + expre1;break;
case 3:expre += "*" +x[1] + expre1;break;
case 4:expre += "/" +x[1] + expre1;break;
case 5:expre += "//" +x[1] + expre1;
}
expre = expre.replace(a,(char)(numb[0] + 48));
expre = expre.replace(b,(char)(numb[1] + 48));
expre = expre.replace(c,(char)(numb[2] + 48));
expre = expre.replace(d,(char)(numb[3] + 48));
System.out.println(expre+" ");
}
//System.out.println("total : " + total[s] + " index :" +"\n\n");
index ++ ;
}
}
}System.out.println("The number of total successful expressions is : " + totalSuc);
}
}