fork(6) download
  1. final class opAlgo
  2. {
  3. // private static final int[] values = {3, 7, 8, 10, 50, 100};
  4. private static final int[] values = {1, 2, 3};
  5. private static final char[] ops = {'+', '-', '/', '*'};
  6.  
  7. public static void main (String[] args)
  8. {
  9. try(java.io.BufferedReader br = new java.io.BufferedReader(
  10. new java.io.InputStreamReader(System.in))) {
  11.  
  12. opAlgo solver = new opAlgo();
  13.  
  14. while (true) {
  15. System.out.print("Enter a target value: ");
  16. int target = Integer.parseInt(br.readLine());
  17. System.out.println(solver.solve(target));
  18. break;
  19. }
  20. }
  21. catch (java.io.IOException | java.lang.IllegalArgumentException e) {
  22.  
  23. }
  24.  
  25. }
  26.  
  27. private String solve(final int target) {
  28. java.lang.StringBuilder ans = new java.lang.StringBuilder();
  29. java.util.LinkedHashSet<Integer> set = new java.util.LinkedHashSet<Integer>();
  30. java.util.List<Character> operands= new java.util.Vector<Character>();
  31.  
  32. for (int n : opAlgo.values) {
  33. set.add(n);
  34. if (n == target) {
  35. ans.append('\n' + Integer.toString(n));
  36. }
  37. looper(n, target, ans, set, operands);
  38. set.remove(n);
  39. }
  40. ans.append("\n-------------------------\n");
  41. return ans.toString();
  42. }
  43.  
  44. private void looper(int total, int target, java.lang.StringBuilder ans,
  45. java.util.LinkedHashSet<Integer> set, java.util.List<Character> operands) {
  46.  
  47. for (int n : opAlgo.values) {
  48. if (set.add(n)) {
  49. for (char op : opAlgo.ops) {
  50. operands.add(op);
  51. int cul = chrToValue(total, op, n);
  52. if (cul == target) {
  53. String exp = opAlgoFormat(set, operands);
  54. ans.append('\n' + exp);
  55. }
  56. looper(cul, target, ans, set, operands);
  57. operands.remove(operands.size() - 1);
  58. }
  59. set.remove(n);
  60. }
  61. }
  62. }
  63.  
  64. private String opAlgoFormat(java.util.LinkedHashSet<Integer> set,
  65. java.util.List<Character> values) {
  66.  
  67. java.util.Iterator<Integer> iter = set.iterator();
  68. String toString = new String(Integer.toString(iter.next()));
  69. int t = 0;
  70. char prev = ' ';
  71. while (iter.hasNext()) {
  72. int v = iter.next();
  73. if ((values.get(t) == '/' || values.get(t) == '*') && (prev == '+' || prev == '-')) {
  74. toString = "(" + toString + ")";
  75. }
  76.  
  77. toString += " " + Character.toString(values.get(t)) + " " + Integer.toString(v);
  78. prev = values.get(t++);
  79. }
  80. return toString;
  81. }
  82.  
  83. private int chrToValue(int n1, char op, int n2) {
  84. switch(op) {
  85. case '+':
  86. return n1 + n2;
  87. case '-':
  88. return n1 - n2;
  89. case '/':
  90. if (n1 % n2 == 0)
  91. return n1 / n2;
  92. return Integer.MAX_VALUE;
  93. case '*':
  94. return n1 * n2;
  95. }
  96. throw new java.lang.IllegalArgumentException();
  97. }
  98. }
Success #stdin #stdout 0.07s 381248KB
stdin
6
stdout
Enter a target value: 
1 + 2 + 3
1 * 2 * 3
1 + 3 + 2
1 * 3 * 2
2 + 1 + 3
2 / 1 * 3
2 * 1 * 3
2 + 3 + 1
2 * 3
2 * 3 / 1
2 * 3 * 1
3 + 1 + 2
3 / 1 * 2
3 * 1 * 2
3 + 2 + 1
3 * 2
3 * 2 / 1
3 * 2 * 1
-------------------------