给4个1至9的整数,计算出通过加减乘除四则运算得到24的所有组合
public class TwentyFour {
public TwentyFour src;
public TwentyFour dst;
public int op; //0,1,2,3 + - * /
public int value;
public boolean type; // 0,基本型 1组合型
public String getString() {
if(type) {
StringBuilder sb = new StringBuilder();
String opStr = null;
switch(op) {
case 0:
opStr = "+";
break;
case 1:
opStr = "-";
break;
case 2:
opStr = "*";
break;
case 3:
opStr = "/";
break;
default:
break;
}
// sb.append("(").append(src.getString()).append(opStr).append(dst.getString()).append(")");
if(src.type && src.op<2 && op>1)
sb.append("(").append(src.getString()).append(")");
else
sb.append(src.getString());
sb.append(opStr);
if(dst.type && (op==1&&dst.op<2 || op==2 && dst.op<2 || op==3) )
sb.append("(").append(dst.getString()).append(")");
else
sb.append(dst.getString());
return sb.toString();
}
else
return Integer.toString(value);
}
}
//
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TwentyFourCalc {
public static List<TwentyFour> getCompose( TwentyFour data1, TwentyFour data2) {
List<TwentyFour> calcList = new ArrayList<>();
int src = data1.value;
int dst = data2.value;
//+
TwentyFour twentyFourTmp = new TwentyFour();
twentyFourTmp.src = data1;
twentyFourTmp.dst = data2;
twentyFourTmp.op = 0;
twentyFourTmp.value = src + dst;
twentyFourTmp.type = true;
calcList.add(twentyFourTmp);
//*
twentyFourTmp = new TwentyFour();
twentyFourTmp.src = data1;
twentyFourTmp.dst = data2;
twentyFourTmp.op = 2;
twentyFourTmp.value = src * dst;
twentyFourTmp.type = true;
calcList.add(twentyFourTmp);
//-
twentyFourTmp = new TwentyFour();
if(src>=dst) {
twentyFourTmp.src = data1;
twentyFourTmp.dst = data2;
twentyFourTmp.value = src - dst;
}
else {
twentyFourTmp.src = data2;
twentyFourTmp.dst = data1;
twentyFourTmp.value = dst - src;
}
twentyFourTmp.op = 1;
twentyFourTmp.type = true;
calcList.add(twentyFourTmp);
//除
if(src!=0&&dst!=0){
twentyFourTmp = new TwentyFour();
int big,small;
if(src>=dst) {
twentyFourTmp.src = data1;
twentyFourTmp.dst = data2;
twentyFourTmp.value = src / dst;
big = src;
small = dst;
}
else {
twentyFourTmp.src = data2;
twentyFourTmp.dst = data1;
twentyFourTmp.value = dst / src;
big = dst;
small = src;
}
if(big%small==0) {
//剩余的项
twentyFourTmp.op = 3;
twentyFourTmp.type = true;
calcList.add(twentyFourTmp);
}
}
else if(src!=0&&dst==0) {
twentyFourTmp = new TwentyFour();
twentyFourTmp.src = data2;
twentyFourTmp.dst = data1;
twentyFourTmp.value = 0;
twentyFourTmp.op = 3;
twentyFourTmp.type = true;
calcList.add(twentyFourTmp);
}
else if(src==0&&dst!=0) {
twentyFourTmp = new TwentyFour();
twentyFourTmp.src = data1;
twentyFourTmp.dst = data2;
twentyFourTmp.value = 0;
twentyFourTmp.op = 3;
twentyFourTmp.type = true;
calcList.add(twentyFourTmp);
}
return calcList;
}
public static void main(String[] args) {
int N = 4;
// int input[] = {3,4,5,8};
int input[] = {2,1,6,8};
TwentyFour twentyFourArray[] = new TwentyFour[N];
//init
for(int i=0;i<N;i++){
TwentyFour twentyFour = new TwentyFour();
twentyFour.value = input[i];
twentyFourArray[i] = twentyFour;
}
List<TwentyFour[]> targetDataList = new ArrayList<>();
targetDataList.add(twentyFourArray); //起始数组
for(int cnt=0;cnt<N-2;cnt++){
List<TwentyFour[]> tmpDataList = new ArrayList<>();
for(TwentyFour[] tmpArr : targetDataList) {
for(int i=0;i<N-1-cnt;i++){
for(int j=i+1;j<N-cnt;j++){
List<TwentyFour> composeList = getCompose(tmpArr[i], tmpArr[j]);
for(TwentyFour twentyFourTmp : composeList) {
//剩余的项
TwentyFour[] twentyFourArrayTmp = new TwentyFour[N-1-cnt];
twentyFourArrayTmp[0] = twentyFourTmp; //组合项
int index = 1;
for(int k=0;k<N-cnt;k++) {
if(k!=i&&k!=j) {
twentyFourArrayTmp[index] = tmpArr[k];
index++;
}
}
tmpDataList.add(twentyFourArrayTmp);
}
}
}
}
targetDataList.clear();
targetDataList.addAll(tmpDataList);//换成新的数组
}
//result
System.out.println("-----------------------------------");
Map<String, TwentyFour> map = new HashMap<>();
List<TwentyFour> oneDataList = new ArrayList<>();
for(TwentyFour[] tmpArr : targetDataList) {
List<TwentyFour> composeList = getCompose(tmpArr[0], tmpArr[1]);
for(TwentyFour twentyFourTmp : composeList) {
oneDataList.add(twentyFourTmp); //
String str = twentyFourTmp.getString();
if(twentyFourTmp.value == 24) {
if(map.get(str)==null) {
map.put(str, twentyFourTmp);
System.out.print( str + " " + twentyFourTmp.value + " ");
System.out.println();
}
}
}
}
}
}