大家可能都见过以下的问题
给定0-9之间的四个数,通过加、减、乘、除运算得出结果24
可以使用括号
例如:三个5一个1
5 x (5 - 1/5) = 24
还有3、4、5、6,大家可以试着算一下
下面是我用java写的一个程序
程序后面是表达式,它需要从文件读入
package compute24;
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();
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器