大家可能都见过以下的问题
给定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
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/28416/showart_222486.html |