1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| import java.util.ArrayList;
public class unittest
{
public static void main(String[] argStrings)
{
long start = System.currentTimeMillis();
ArrayList<String> allopts = new ArrayList<String>();
int a,b,c,d,e,f,g,h;
for(a = 0;a < 3;a++)
for(b = 0;b < 3;b++)
for(c = 0;c < 3;c++)
for(d = 0;d < 3;d++)
for(e = 0;e < 3;e++)
for(f = 0;f < 3;f++)
for(g = 0;g < 3;g++)
for(h = 0;h < 3;h++) {
allopts.add(""+a+b+c+d+e+f+g+h);
}
for (String string : allopts)
{
String base = "1x2x3x4x5x6x7x8x9";
for (int i = 0; i < string.length(); i++)
{
String tmp = getOpt(Integer.parseInt(""+string.charAt(i)));
base = base.replaceFirst("x", tmp);
}
int x = getResult(base, string.replaceAll("2", "").replaceAll("0", "+").replaceAll("1", "-"));//2->null
if (x == 110)
{
System.out.println(base);
}
}
System.out.println("All Run Time: " + (System.currentTimeMillis() - start) + "ms");
}
public static String getOpt(int opt) {
String retString = "";
switch (opt)
{
case 0:
retString = "+";
break;
case 1:
retString = "-";
break;
case 2:
retString = "";
break;
default:
break;
}
return retString;
}
public static int getResult(String calculatString, String optString)
{
ArrayList<Integer> allNum = new ArrayList<Integer>();
ArrayList<String> allOpt = new ArrayList<String>();
for (String string : calculatString.split("[+-]"))
{
allNum.add(Integer.parseInt(string));
}
for (int i = 0; i < optString.length(); i++)
{
allOpt.add(optString.charAt(i) + "");
}
int result = allNum.get(0);
for (int i = 1; i < allNum.size(); i++)
{
char x = (i - 1) == allOpt.size()?'+':allOpt.get(i - 1).charAt(0);
int nextNum = i == allNum.size()?0:allNum.get(i);
result = getValue(result, nextNum, x);
}
return result;
}
public static int getValue(Integer num1, Integer num2, char opt)
{
int ret = 0;
switch (opt)
{
case '+':
ret = num1 + num2;
break;
case '-':
ret = num1 - num2;
break;
default:
break;
}
return ret;
}
}
|