Skip to content

Commit 02793ec

Browse files
authored
Merge pull request #888 from AlgorithmWithGod/zinnnn37
[20250914] BOJ / G5 / 0 만들기 / 김민진
2 parents 4d7d1b5 + f01ce2c commit 02793ec

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
```java
2+
package etc;
3+
4+
import java.io.*;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class BJ_7490_0_만들기 {
9+
10+
private static final String OPS = " +-";
11+
12+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
private static final StringBuilder sb = new StringBuilder();
15+
16+
private static int N;
17+
private static char[] ops;
18+
19+
public static void main(String[] args) throws IOException {
20+
int T = Integer.parseInt(br.readLine());
21+
ops = new char[10];
22+
23+
while (T-- > 0) {
24+
sol();
25+
if (T > 0) {
26+
sb.append("\n");
27+
}
28+
}
29+
bw.write(sb.toString());
30+
bw.flush();
31+
bw.close();
32+
br.close();
33+
}
34+
35+
private static void sol() throws IOException {
36+
N = Integer.parseInt(br.readLine());
37+
38+
for (int i = 0; i < 3; i++) {
39+
char o = OPS.charAt(i);
40+
ops[2] = o;
41+
dfs(2);
42+
}
43+
}
44+
45+
private static void dfs(int num) {
46+
if (num == N) {
47+
if (isValid()) {
48+
appendString();
49+
}
50+
return;
51+
}
52+
53+
for (int i = 0; i < 3; i++) {
54+
char o = OPS.charAt(i);
55+
ops[num + 1] = o;
56+
dfs(num + 1);
57+
}
58+
}
59+
60+
private static boolean isValid() {
61+
List<Integer> nums = new ArrayList<>();
62+
63+
// 공백 처리
64+
int idx = 0;
65+
while (++idx <= N) {
66+
int num = idx;
67+
while (idx < N && ops[idx + 1] == ' ') {
68+
idx++;
69+
num = num * 10 + idx;
70+
}
71+
nums.add(num);
72+
}
73+
74+
int res = nums.get(0);
75+
int opIdx = 2;
76+
77+
for (int i = 1; i < nums.size(); i++) {
78+
// 공백 건너뛰기
79+
while (opIdx <= N && ops[opIdx] == ' ') opIdx++;
80+
81+
if (ops[opIdx] == '+') {
82+
res += nums.get(i);
83+
} else {
84+
res -= nums.get(i);
85+
}
86+
opIdx++;
87+
}
88+
89+
return res == 0;
90+
}
91+
92+
private static void appendString() {
93+
for (int i = 1; i < N; i++) {
94+
sb.append(i).append(ops[i + 1]);
95+
}
96+
sb.append(N).append('\n');
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)