final class opAlgo
{
// private static final int[] values = {3, 7, 8, 10, 50, 100};
private static final int[] values = {1, 2, 3};
private static final char[] ops = {'+', '-', '/', '*'};
public static void main
(String[] args
) {
opAlgo solver = new opAlgo();
while (true) {
System.
out.
print("Enter a target value: "); int target
= Integer.
parseInt(br.
readLine()); System.
out.
println(solver.
solve(target
)); break;
}
}
}
}
private String solve
(final int target
) { java.lang.StringBuilder ans = new java.lang.StringBuilder();
java.util.LinkedHashSet<Integer> set = new java.util.LinkedHashSet<Integer>();
java.util.List<Character> operands= new java.util.Vector<Character>();
for (int n : opAlgo.values) {
set.add(n);
if (n == target) {
ans.
append('\n' + Integer.
toString(n
)); }
looper(n, target, ans, set, operands);
set.remove(n);
}
ans.append("\n-------------------------\n");
return ans.toString();
}
private void looper(int total, int target, java.lang.StringBuilder ans,
java.util.LinkedHashSet<Integer> set, java.util.List<Character> operands) {
for (int n : opAlgo.values) {
if (set.add(n)) {
for (char op : opAlgo.ops) {
operands.add(op);
int cul = chrToValue(total, op, n);
if (cul == target) {
String exp
= opAlgoFormat
(set, operands
); ans.append('\n' + exp);
}
looper(cul, target, ans, set, operands);
operands.remove(operands.size() - 1);
}
set.remove(n);
}
}
}
private String opAlgoFormat
(java.
util.
LinkedHashSet<Integer
> set,
java.util.List<Character> values) {
java.util.Iterator<Integer> iter = set.iterator();
int t = 0;
char prev = ' ';
while (iter.hasNext()) {
int v = iter.next();
if ((values.get(t) == '/' || values.get(t) == '*') && (prev == '+' || prev == '-')) {
toString = "(" + toString + ")";
}
toString
+= " " + Character.
toString(values.
get(t
)) + " " + Integer.
toString(v
); prev = values.get(t++);
}
return toString;
}
private int chrToValue(int n1, char op, int n2) {
switch(op) {
case '+':
return n1 + n2;
case '-':
return n1 - n2;
case '/':
if (n1 % n2 == 0)
return n1 / n2;
case '*':
return n1 * n2;
}
}
}
ZmluYWwgY2xhc3Mgb3BBbGdvCnsKICAvLyAgICBwcml2YXRlIHN0YXRpYyBmaW5hbCBpbnRbXSB2YWx1ZXMgPSB7MywgNywgOCwgMTAsIDUwLCAxMDB9OwogIHByaXZhdGUgc3RhdGljIGZpbmFsIGludFtdIHZhbHVlcyA9IHsxLCAyLCAzfTsKICBwcml2YXRlIHN0YXRpYyBmaW5hbCBjaGFyW10gb3BzID0geycrJywgJy0nLCAnLycsICcqJ307CgogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKQogIHsKICAgICAgdHJ5KGphdmEuaW8uQnVmZmVyZWRSZWFkZXIgYnIgPSBuZXcgamF2YS5pby5CdWZmZXJlZFJlYWRlcigKICAgICAgICAgICAgICBuZXcgamF2YS5pby5JbnB1dFN0cmVhbVJlYWRlcihTeXN0ZW0uaW4pKSkgewoKICAgICAgICAgIG9wQWxnbyBzb2x2ZXIgPSBuZXcgb3BBbGdvKCk7CgogICAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50KCJFbnRlciBhIHRhcmdldCB2YWx1ZTogIik7CiAgICAgICAgICAgICAgaW50IHRhcmdldCA9IEludGVnZXIucGFyc2VJbnQoYnIucmVhZExpbmUoKSk7CiAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHNvbHZlci5zb2x2ZSh0YXJnZXQpKTsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgfQogICAgICBjYXRjaCAoamF2YS5pby5JT0V4Y2VwdGlvbiB8IGphdmEubGFuZy5JbGxlZ2FsQXJndW1lbnRFeGNlcHRpb24gZSkgewoKICAgICAgfQoKICB9CgogIHByaXZhdGUgU3RyaW5nIHNvbHZlKGZpbmFsIGludCB0YXJnZXQpIHsKICAgICAgamF2YS5sYW5nLlN0cmluZ0J1aWxkZXIgYW5zID0gbmV3IGphdmEubGFuZy5TdHJpbmdCdWlsZGVyKCk7CiAgICAgIGphdmEudXRpbC5MaW5rZWRIYXNoU2V0PEludGVnZXI+IHNldCA9IG5ldyBqYXZhLnV0aWwuTGlua2VkSGFzaFNldDxJbnRlZ2VyPigpOwogICAgICBqYXZhLnV0aWwuTGlzdDxDaGFyYWN0ZXI+IG9wZXJhbmRzPSBuZXcgamF2YS51dGlsLlZlY3RvcjxDaGFyYWN0ZXI+KCk7CgogICAgICBmb3IgKGludCBuIDogb3BBbGdvLnZhbHVlcykgewogICAgICAgICAgc2V0LmFkZChuKTsKICAgICAgICAgIGlmIChuID09IHRhcmdldCkgewogICAgICAgICAgICAgIGFucy5hcHBlbmQoJ1xuJyArIEludGVnZXIudG9TdHJpbmcobikpOwogICAgICAgICAgfQogICAgICAgICAgbG9vcGVyKG4sIHRhcmdldCwgYW5zLCBzZXQsIG9wZXJhbmRzKTsKICAgICAgICAgIHNldC5yZW1vdmUobik7CiAgICAgIH0KICAgICAgYW5zLmFwcGVuZCgiXG4tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4iKTsKICAgICAgcmV0dXJuIGFucy50b1N0cmluZygpOwogIH0KCiAgcHJpdmF0ZSB2b2lkIGxvb3BlcihpbnQgdG90YWwsIGludCB0YXJnZXQsIGphdmEubGFuZy5TdHJpbmdCdWlsZGVyIGFucywgCiAgICAgICAgICBqYXZhLnV0aWwuTGlua2VkSGFzaFNldDxJbnRlZ2VyPiBzZXQsIGphdmEudXRpbC5MaXN0PENoYXJhY3Rlcj4gb3BlcmFuZHMpIHsKCiAgICAgIGZvciAoaW50IG4gOiBvcEFsZ28udmFsdWVzKSB7CiAgICAgICAgICBpZiAoc2V0LmFkZChuKSkgewogICAgICAgICAgICAgIGZvciAoY2hhciBvcCA6IG9wQWxnby5vcHMpIHsKICAgICAgICAgICAgICAgICAgb3BlcmFuZHMuYWRkKG9wKTsKICAgICAgICAgICAgICAgICAgaW50IGN1bCA9IGNoclRvVmFsdWUodG90YWwsIG9wLCBuKTsKICAgICAgICAgICAgICAgICAgaWYgKGN1bCA9PSB0YXJnZXQpIHsKICAgICAgICAgICAgICAgICAgICAgIFN0cmluZyBleHAgPSBvcEFsZ29Gb3JtYXQoc2V0LCBvcGVyYW5kcyk7CiAgICAgICAgICAgICAgICAgICAgICBhbnMuYXBwZW5kKCdcbicgKyBleHApOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGxvb3BlcihjdWwsIHRhcmdldCwgYW5zLCBzZXQsIG9wZXJhbmRzKTsKICAgICAgICAgICAgICAgICAgb3BlcmFuZHMucmVtb3ZlKG9wZXJhbmRzLnNpemUoKSAtIDEpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBzZXQucmVtb3ZlKG4pOwogICAgICAgICAgfQogICAgICB9CiAgfQoKICBwcml2YXRlIFN0cmluZyBvcEFsZ29Gb3JtYXQoamF2YS51dGlsLkxpbmtlZEhhc2hTZXQ8SW50ZWdlcj4gc2V0LCAKICAgICAgICAgIGphdmEudXRpbC5MaXN0PENoYXJhY3Rlcj4gdmFsdWVzKSB7CgogICAgICBqYXZhLnV0aWwuSXRlcmF0b3I8SW50ZWdlcj4gaXRlciA9IHNldC5pdGVyYXRvcigpOwogICAgICBTdHJpbmcgdG9TdHJpbmcgPSBuZXcgU3RyaW5nKEludGVnZXIudG9TdHJpbmcoaXRlci5uZXh0KCkpKTsKICAgICAgaW50IHQgPSAwOwogICAgICBjaGFyIHByZXYgPSAnICc7CiAgICAgIHdoaWxlIChpdGVyLmhhc05leHQoKSkgewogICAgICAgICAgaW50IHYgPSBpdGVyLm5leHQoKTsKICAgICAgICAgIGlmICgodmFsdWVzLmdldCh0KSA9PSAnLycgfHwgdmFsdWVzLmdldCh0KSA9PSAnKicpICYmIChwcmV2ID09ICcrJyB8fCBwcmV2ID09ICctJykpIHsKICAgICAgICAgICAgICB0b1N0cmluZyA9ICIoIiArIHRvU3RyaW5nICsgIikiOwogICAgICAgICAgfQoKICAgICAgICAgIHRvU3RyaW5nICs9ICIgIiArIENoYXJhY3Rlci50b1N0cmluZyh2YWx1ZXMuZ2V0KHQpKSArICIgIiArIEludGVnZXIudG9TdHJpbmcodik7CiAgICAgICAgICBwcmV2ID0gdmFsdWVzLmdldCh0KyspOwogICAgICB9CiAgICAgIHJldHVybiB0b1N0cmluZzsKICB9CgogIHByaXZhdGUgaW50IGNoclRvVmFsdWUoaW50IG4xLCBjaGFyIG9wLCBpbnQgbjIpIHsKICAgICAgc3dpdGNoKG9wKSB7CiAgICAgIGNhc2UgJysnOgogICAgICAgICAgcmV0dXJuIG4xICsgbjI7CiAgICAgIGNhc2UgJy0nOgogICAgICAgICAgcmV0dXJuIG4xIC0gbjI7CiAgICAgIGNhc2UgJy8nOgogICAgICAgICAgaWYgKG4xICUgbjIgPT0gMCkKICAgICAgICAgICAgICByZXR1cm4gbjEgLyBuMjsKICAgICAgICAgIHJldHVybiBJbnRlZ2VyLk1BWF9WQUxVRTsKICAgICAgY2FzZSAnKic6CiAgICAgICAgICByZXR1cm4gbjEgKiBuMjsKICAgICAgfQogICAgICB0aHJvdyBuZXcgamF2YS5sYW5nLklsbGVnYWxBcmd1bWVudEV4Y2VwdGlvbigpOwogIH0KfQ==