https://www.acmicpc.net/problem/7490
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
static int n;
static String[] operator = {"+", "-", " "};
static ArrayList<String> expression;
public boolean calculate(String exp) {
StringTokenizer st = new StringTokenizer(exp, "+|-", true);
int result = Integer.parseInt(st.nextToken());
while(st.hasMoreTokens()) {
String temp = st.nextToken();
if(temp.equals("+")) {
result += Integer.parseInt(st.nextToken());
} else {
result -= Integer.parseInt(st.nextToken());
}
}
if(result == 0) {
return true;
}
return false;
}
public void getAllCase(int num, String e) {
if(num == n) {
String exp = e.replaceAll(" ", "");
if(calculate(exp)) {
expression.add(e);
}
return;
}
for(int i = 0; i < 3; i++) {
getAllCase(num + 1, e + operator[i] + Integer.toString(num + 1));
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Main m = new Main();
int test_num = Integer.parseInt(br.readLine());
for(int i = 0; i < test_num; i++) {
n = Integer.parseInt(br.readLine());
expression = new ArrayList<>();
m.getAllCase(1, "1");
Collections.sort(expression);
for(String e : expression) {
bw.write(e + "\n");
}
bw.write("\n");
}
br.close();
bw.flush();
bw.close();
}
}