- import java.util.ArrayList;
- import java.util.List;
- class MathDemo {
- public int Mjia(int a, int b) {
- return a + b;
- }
- public int Mjian(int a, int b) {
- return a - b;
- }
- public int Mcheng(int a, int b) {
- return a * b;
- }
- public int Mchu(int a, int b) {
- return a / b;
- }
- }
- public class TTF_24 {
- public static String ttfDemo(List<Integer> numbers) {
- MathDemo md = new MathDemo();
- for (int num : numbers) {
- int[] ynums = new int[4];
- int weishu = 1000;
- int len = 0;
- while (weishu > 0) {
- ynums[len] = num / weishu;
- num = num % weishu;
- weishu = weishu / 10;
- len++;
- }
- for (int i = 0; i < 4; i++) {
- for (int k = 0; k < 4; k++) {
- for (int j = 0; j < 4; j++) {
- int a = 0;
- int b = 0;
- int c = 0;
- switch (i) {
- case 0:
- a = md.Mjia(ynums[0], ynums[1]);
- break;
- case 1:
- a = md.Mjian(ynums[0], ynums[1]);
- break;
- case 2:
- a = md.Mcheng(ynums[0], ynums[1]);
- break;
- case 3:
- a = md.Mchu(ynums[0], ynums[1]);
- break;
- }
- switch (k) {
- case 0:
- b = md.Mjia(a, ynums[2]);
- break;
- case 1:
- b = md.Mjian(a, ynums[2]);
- break;
- case 2:
- b = md.Mcheng(a, ynums[2]);
- break;
- case 3:
- b = md.Mchu(a, ynums[2]);
- break;
- }
- switch (j) {
- case 0:
- c = md.Mjia(b, ynums[3]);
- break;
- case 1:
- c = md.Mjian(b, ynums[3]);
- break;
- case 2:
- c = md.Mcheng(b, ynums[3]);
- break;
- case 3:
- c = md.Mchu(b, ynums[3]);
- break;
- }
- if (c == 24) {
- String f1 = "";
- String f2 = "";
- String f3 = "";
- switch (i) {
- case 0:
- f1 = "+";
- break;
- case 1:
- f1 = "-";
- break;
- case 2:
- f1 = "*";
- break;
- case 3:
- f1 = "/";
- break;
- }
- switch (k) {
- case 0:
- f2 = "+";
- break;
- case 1:
- f2 = "-";
- break;
- case 2:
- f2 = "*";
- break;
- case 3:
- f2 = "/";
- break;
- }
- switch (j) {
- case 0:
- f3 = "+";
- break;
- case 1:
- f3 = "-";
- break;
- case 2:
- f3 = "*";
- break;
- case 3:
- f3 = "/";
- break;
- }
- return "(((" + ynums[0] + f1 + ynums[1] + ")" + f2
- + ynums[2] + ")" + f3 + ynums[3] + "=24";
- }
- }
- }
- }
- }
- return "No answer!";
- }
- public static Integer newNum(int nums, int num) throws Exception {
- int[] ynums = new int[4];
- int[] hnums = new int[4];
- int[] nnumber = new int[4];
- int weishu = 1000;
- int len = 0;
- while (weishu > 0) {
- ynums[len] = num / weishu;
- num = num % weishu;
- weishu = weishu / 10;
- len++;
- }
- int weishu1 = 1000;
- int len1 = 0;
- while (weishu1 > 0) {
- hnums[len1] = nums / weishu1;
- nums = nums % weishu1;
- weishu1 = weishu1 / 10;
- len1++;
- }
- int len2 = 0;
- for (int wei : ynums) {
- nnumber[len2] = hnums[wei - 1];
- len2++;
- }
- return nnumber[0] * 1000 + nnumber[1] * 100 + nnumber[2] * 10
- + nnumber[3];
- }
- public static List<Integer> arrayNum(int nums) throws Exception {
- List<Integer> arrayNumber = new ArrayList<Integer>();
- int[] xulie = new int[] { 1234, 1243, 1324, 1342, 1423, 1432, 2134,
- 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412,
- 3421, 4123, 4132, 4213, 4231, 4312, 4321 };
- for (int i : xulie) {
- arrayNumber.add(newNum(nums, i));
- }
- return arrayNumber;
- }
- public static void main(String[] args) throws Exception {
- int[] num = new int[4];
- int nums = Integer.parseInt(javax.swing.JOptionPane
- .showInputDialog("请输入四个1-9的整数"));
- System.out.println(ttfDemo(arrayNum(nums)));
- }
- }
复制代码 输入:1259
结果:(((1+2)*5)+9=24
写的有点粗糙对付着看吧!~ |