-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
38 lines (28 loc) · 1.11 KB
/
Main.java
File metadata and controls
38 lines (28 loc) · 1.11 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
for(int i=0; i<testCase; i++){
N = Integer.parseInt(br.readLine());
solution(0, 1, 1, 1, "1");
System.out.println();
}
br.close();
}
public static void solution(int sum, int sign, int num, int cnt, String result) {
if(cnt == N){
sum = sum + (sign*num);
if( sum == 0){
System.out.println(result);
}
return;
}
solution(sum, sign, num*10+(cnt+1), cnt+1, result + " " +Integer.toString(cnt+1));
solution(sum+(sign*num), 1, cnt+1, cnt+1, result + "+" +Integer.toString(cnt+1));
solution(sum+(sign*num), -1, cnt+1, cnt+1, result + "-" +Integer.toString(cnt+1));
}
}