给定0-9之间的四个数,通过加、减、乘、除运算得出结果24
可以使用括号
例如:三个5一个1
5 x (5 - 1/5) = 24
还有3、4、5、6,大家可以试着算一下
下面是我用java写的一个程序
程序后面是表达式,它需要从文件读入
package compute24;Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 大家可能都见过以下的问题
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Compute24 {
static float[][] expression = new float[40][15];
static int[][] fourNumber = new int[24][4];
static int[] fourNum = { 0, 5, 5, 5, 1 };
//可以在5551位置输入要计算的四个数
public static void main(String[] args) throws IOException {
// this.readExpressionFromFile();
Compute24 obj1 = new Compute24();
obj1.readExpressionFromFile();
obj1.arrangeFourNumber();
obj1.insertNumberToExpression();
// obj1.computeExpression(expression);
}
public void arrangeFourNumber() {
/* arrange the four number */
/*
* make a canzhao array
*/
int[][] compare = { { 1, 2, 3, 4 }, { 1, 3, 2, 4 }, { 1, 3, 4, 2 },
{ 1, 2, 4, 3 }, { 1, 4, 2, 3 }, { 1, 4, 3, 2 }, { 2, 1, 3, 4 },
{ 2, 1, 4, 3 }, { 2, 3, 1, 4 }, { 2, 3, 4, 1 }, { 2, 4, 3, 1 },
{ 2, 4, 1, 3 }, { 3, 1, 2, 4 }, { 3, 1, 4, 2 }, { 3, 2, 1, 4 },
{ 3, 2, 4, 1 }, { 3, 4, 2, 1 }, { 3, 4, 1, 2 }, { 4, 1, 2, 3 },
{ 4, 1, 3, 2 }, { 4, 2, 1, 3 }, { 4, 2, 3, 1 }, { 4, 3, 1, 2 },
{ 4, 3, 2, 1 } };
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
fourNumber[i][j] = fourNum[compare[i][j]];
}
}
/*
* search a easy way to compute the combination of four number
*/
}
public void readExpressionFromFile() throws IOException {
int getChar;
FileInputStream fin;
try {
fin = new FileInputStream(
"D:\JavaCode\Compute24\compute24\expression.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found");
return;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage:showFile File");
return;
}
for (int i = 0; i < 38; i++) {
for (int j = 0; j < 15; j++) {
getChar = fin.read();
expression[i][j] = getChar;
if (getChar == 48)
break;
}
}
// how to read form File?
}
public void insertNumberToExpression() {
/*
* insert the 24 group number into the expression
*/
for (int sign = 0; sign < 24; sign++) {/*
* sign to help inserting 24
* pairs of four number
*/
for (int i = 0; i < 14; i++) {
for (int j = 0, k = 0; k < 7; j++, k = k + 2)
expression[i][k] = fourNumber[sign][j];
}
for (int i = 14; i < 24; i++) {
for (int j = 0, k = 1; k < 9; j++, k = k + 2) {
expression[i][k] = fourNumber[sign][j];
if (k == 3)
k++;
}
}
for (int i = 24; i < 26; i++) {
for (int j = 0, k = 0; k < 8; j++, k = k + 2) {
expression[i][k] = fourNumber[sign][j];
if (k == 2)
k++;
}
}
for (int i = 26; i < 34; i++) {
for (int j = 0, k = 1; k < 9; j++, k = k + 2) {
expression[i][k] = fourNumber[sign][j];
if (k == 5)
k++;
}
}
for (int i = 34; i < 35; i++) {
for (int j = 0, k = 0; k < 9; j++, k = k + 2) {
expression[i][k] = fourNumber[sign][j];
if (k == 0)
k++;
}
}
for (int i = 35; i < 38; i++) {
for (int j = 0, k = 1; k < 10; j++, k = k + 2) {
expression[i][k] = fourNumber[sign][j];
if (k == 3)
k = k + 2;
}
}
this.computeExpression();
}
}
给定0-9之间的四个数,通过加、减、乘、除运算得出结果24
可以使用括号
例如:三个5一个1
5 x (5 - 1/5) = 24
还有3、4、5、6,大家?
标签: