import java.io.*;
public class NNumCalculateToM {
final static double PRECISION = 1E-6;
final static int NUM = 4;
final static int RESULT = 24;
static double number[];
static String[] expression;
static boolean circule(int n)
{
if(n == 1)
{
if(Math.abs(number[0] - RESULT) < PRECISION)
{
System.out.println(expression[0]);
return true;
}
else
{
return false;
}
}
for(int i=0; i
for(int j=i+1; j
{
double numi,numj;
String expi,expj;
numi = number[i];
numj = number[j];
number[j] = number[n-1];
expi = expression[i];
expj = expression[j];
expression[j] = expression[n-1];
//加的处理
if(n > 2)
expression[i] = "(" + expi + "+" + expj + ")";
else
expression[i] = expi + "+" + expj;
number[i] = numi + numj;
if(circule(n-1))
return true;
//减的处理,有两种情况expi-expj,expj-expi
if(n > 2)
expression[i] = '(' + expi + '-' + expj + ')';
else
expression[i] = expi + '-' + expj;
number[i] = numi - numj;
if(circule(n-1))
return true;
if(n > 2)
expression[i] = '(' + expj + '-' + expi + ')';
else
expression[i] = expj + '-' + expi;
number[i] = numj - numi;
if(circule(n-1))
return true;
//乘的处理
expression[i] = expi + "*" + expj;
number[i] = numi * numj;
if(circule(n-1))
return true;
//除的处理,有两种情况expi/expj,expj/expi
if(numj > PRECISION)
{
expression[i] = expi + '/' + expj;
number[i] = numi / numj;
if(circule(n-1))
return true;
}