Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions JHLEE325/202602/04 BOJ G5 0 만들기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
```java
import java.io.*;
import java.util.*;

public class Main {

static StringBuilder sb = new StringBuilder();
static int N;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());

for (int t = 0; t < T; t++) {
N = Integer.parseInt(br.readLine());
bt(1, "1");
sb.append("\n");
}
System.out.print(sb.toString());
}

static void bt(int num, String str) {
if (num == N) {
if (calculate(str) == 0) {
sb.append(str).append("\n");
}
return;
}

int nextNum = num + 1;
bt(nextNum, str + " " + nextNum);
bt(nextNum, str + "+" + nextNum);
bt(nextNum, str + "-" + nextNum);
}

static int calculate(String str) {
String replaced = str.replaceAll(" ", "");

StringTokenizer st = new StringTokenizer(replaced, "+|-", true);

int result = Integer.parseInt(st.nextToken());
while (st.hasMoreTokens()) {
String op = st.nextToken();
int next = Integer.parseInt(st.nextToken());

if (op.equals("+")) result += next;
else result -= next;
}
return result;
}
}
```