|
| 1 | +```java |
| 2 | +import java.io.IOException; |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.io.*; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static int cnt,target,ans; |
| 11 | + static int[] arr; |
| 12 | + static List<Integer> list; |
| 13 | + static String[] decide; |
| 14 | + static StringBuilder sb = new StringBuilder(); |
| 15 | + |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + init(); |
| 19 | + process(); |
| 20 | + print(); |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + public static void init() throws IOException { |
| 25 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 26 | + cnt = Integer.parseInt(br.readLine()); |
| 27 | + arr = new int[cnt]; |
| 28 | + |
| 29 | + |
| 30 | + for (int i = 0; i < cnt; i++) { |
| 31 | + arr[i] = Integer.parseInt(br.readLine()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static void process() throws IOException { |
| 36 | + for (int tc = 0; tc < cnt; tc++) { |
| 37 | + ans = 0; |
| 38 | + target = arr[tc]; |
| 39 | + decide = new String[target]; |
| 40 | + list = new ArrayList<>(); |
| 41 | + list.add(1); |
| 42 | + |
| 43 | + dfs(1); |
| 44 | + |
| 45 | + if (ans != 0) sb.append("\n"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static void dfs(int num) { |
| 50 | + if (num == target) { |
| 51 | + |
| 52 | + calculate(); |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + int nNum = num+1; |
| 59 | + int temp = list.get(list.size()-1); |
| 60 | + |
| 61 | + |
| 62 | + list.remove(list.size()-1); |
| 63 | + decide[num-1] = " "; |
| 64 | + |
| 65 | + if (temp < 0) { |
| 66 | + list.add(temp*10 - num - 1); |
| 67 | + } else { |
| 68 | + list.add(temp*10 + num + 1); |
| 69 | + } |
| 70 | + |
| 71 | + dfs(num+1); |
| 72 | + list.remove(list.size()-1); |
| 73 | + list.add(temp); |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + list.add(num+1); |
| 78 | + decide[num-1] = "+"; |
| 79 | + dfs(num+1); |
| 80 | + list.remove(list.size()-1); |
| 81 | + |
| 82 | + list.add((num+1)*(-1)); |
| 83 | + decide[num-1] = "-"; |
| 84 | + dfs(num+1); |
| 85 | + list.remove(list.size()-1); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + public static void calculate() { |
| 96 | + int res = 0; |
| 97 | + |
| 98 | + for (int n: list) { |
| 99 | + res += n; |
| 100 | + } |
| 101 | + |
| 102 | + if (res == 0) { |
| 103 | + ans++; |
| 104 | + sb.append(1); |
| 105 | + for (int i = 1; i < target; i++) { |
| 106 | + sb.append(decide[i-1]).append(i+1); |
| 107 | + } |
| 108 | + sb.append("\n"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + public static void print() { |
| 115 | + System.out.println(sb.toString()); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +``` |
0 commit comments