-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombine4.java
More file actions
131 lines (104 loc) · 2.33 KB
/
Combine4.java
File metadata and controls
131 lines (104 loc) · 2.33 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.*;
public class Combine4
{
public final static int PLUS = 0;
public final static int MINUS = 1;
public final static int MULTIPLY = 2;
public final static int DIVIDE = 3;
public static void main(String args[])
{
if (args.length < 4)
{
System.out.println("Insufficient number of arguments");
return;
}
combine(buildInitialList(args));
}
public static ArrayList buildInitialList(String args[])
{
ArrayList l = new ArrayList();
for (int i = 0; i < 4; i++)
{
MyElement e = new MyElement();
e.s = args[i];
e.n = Float.parseFloat(e.s);
l.add(e);
}
return l;
}
public static boolean combine(ArrayList l)
{
int len = l.size();
int i, j;
if (len == 1) return checkFinalResult(l);
for (i = 0; i < len - 1; i++)
{
for (j = i + 1; j < len; j++)
{
if (calculate(l, i, j, PLUS)) return true;
if (calculate(l, i, j, MINUS)) return true;
if (calculate(l, j, i, MINUS)) return true;
if (calculate(l, i, j, MULTIPLY)) return true;
if (calculate(l, i, j, DIVIDE)) return true;
if (calculate(l, j, i, DIVIDE)) return true;
}
}
return false;
}
public static ArrayList copyList(ArrayList l)
{
ArrayList l1 = new ArrayList();
int len = l.size();
for (int i = 0; i < len; i++)
{
MyElement temp = new MyElement();
temp.s = ((MyElement)(l.get(i))).s;
temp.n = ((MyElement)(l.get(i))).n;
l1.add(temp);
}
return l1;
}
public static boolean calculate(ArrayList l, int a, int b, int op)
{
ArrayList l1;
int i, len;
MyElement e1, e2;
l1 = copyList(l);
e1 = (MyElement)(l1.get(a));
e2 = (MyElement)(l1.get(b));
switch (op)
{
case PLUS:
e1.n = e1.n + e2.n;
e1.s = "(" + e1.s + "+" + e2.s + ")";
break;
case MINUS:
e1.n = e1.n - e2.n;
e1.s = "(" + e1.s + "-" + e2.s + ")";
break;
case MULTIPLY:
e1.n = e1.n * e2.n;
e1.s = "(" + e1.s + "*" + e2.s + ")";
break;
case DIVIDE:
if (e2.n == 0) return false;
e1.n = e1.n / e2.n;
e1.s = "(" + e1.s + "/" + e2.s + ")";
break;
}
if (e1.n < 0)
return false;
l1.remove(b);
return combine(l1);
}
public static boolean checkFinalResult(ArrayList l)
{
if (((MyElement)l.get(0)).n == 24)
{
System.out.println(((MyElement)l.get(0)).s);
return true;
}
else
return false;
}
}