62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
class Detail {
int value;
String strValue;
public Detail() {
}
public Detail(int value) {
this.value = value;
this.strValue = String.valueOf(value);
}
}
public class C24 {
/**
* @param args
*/
public static void main(String[] args) {
C24 c24 = new C24();
c24.C24p();
}
void C24p() {
int[] srcCards = generateCards();
boolean hasAnswer = false;
// int[] srcCards ={4,1,2,10};
try {
List<int[]> all = allPermutation(srcCards);
List<Detail> results = new ArrayList<Detail>();
for (int[] cards : all) {
List<Detail> oneCase = calEachCase(cards);
results.addAll(oneCase);
}
for (Detail detail : results) {
if (detail.value == 24) {
hasAnswer = true;
System.out.println(detail.strValue);
}
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(Arrays.toString(srcCards));
}
if (!hasAnswer) {
System.out.println("No Answer");
System.out.println(Arrays.toString(srcCards));
}
}
int[] generateCards() {
int[] rtn = new int[4];
Random rand = new Random();
for (int i = 0; i < rtn.length; i++) {
rtn[i] = rand.nextInt(10) + 1;
}
return rtn;
}
List<int[]> allPermutation(int[] cards) {
List<int[]> rtn = new ArrayList<int[]>();
int length = cards.length;
if (length == 1) {
rtn.add(cards);
return rtn;
}
for (int i = 0; i < length; i++) {
int card = cards[i];
int[] leftCards = removeOne(cards, i);
List<int[]> newRtn = allPermutation(leftCards);
for (int[] js : newRtn) {
int[] newCards = new int[length];
newCards[0] = card;
System.arraycopy(js, 0, newCards, 1, length - 1);
rtn.add(newCards);
}
}
return rtn;
}
int[] removeOne(int[] cards, int index) {
int[] newCards = new int[cards.length - 1];
int j = 0;
for (int i = 0; i < cards.length; i++) {
if (index != i) {
newCards[j] = cards[i];
j++;
}
}
return newCards;
}
List<Detail> calEachCase(int[] cards) {
List<Detail> list = new ArrayList<Detail>();
int x = cards[0];
int y = cards[1];
int z = cards[2];
int u = cards[3];
List<Detail> listA = cal(new Detail(x), new Detail(y));
List<Detail> listB = cal(new Detail(z), new Detail(u));
list.addAll(cal(listA, listB));
List<Detail> list1 = cal(new Detail(x), new Detail(y));
List<Detail> list2 = cal(list1, new Detail(z));
List<Detail> list3 = cal(list2, new Detail(u));
list.addAll(list3);
return list;
}
List<Detail> cal(Detail detail1, Detail detail2) {
int x = detail1.value;
int y = detail2.value;
String strX = detail1.strValue;
String strY = detail2.strValue;
List<Detail> rtnList = new ArrayList<Detail>();
Detail detail = null;
detail = new Detail();
detail.value = x + y;
detail.strValue = "(" + strX + "+" + strY + ")";
rtnList.add(detail);
detail = new Detail();
detail.value = x - y;
detail.strValue = "(" + strX + "-" + strY + ")";
rtnList.add(detail);
detail = new Detail();
detail.value = x * y;
detail.strValue = "(" + strX + "*" + strY + ")";
rtnList.add(detail);
if ((y != 0) && ((x % y) == 0)) {
detail = new Detail();
detail.value = x / y;
detail.strValue = "(" + strX + "/" + strY + ")";
rtnList.add(detail);
}
return rtnList;
}
List<Detail> cal(List<Detail> list, Detail detail) {
List<Detail> rtnList = new ArrayList<Detail>();
for (Detail det : list) {
rtnList.addAll(cal(det, detail));
}
return rtnList;
}
List<Detail> cal(List<Detail> list1, List<Detail> list2) {
List<Detail> rtnList = new ArrayList<Detail>();
for (Detail detail1 : list1) {
for (Detail detail2 : list2) {
rtnList.addAll(cal(detail1, detail2));
}
}
return rtnList;
}
}