-
Notifications
You must be signed in to change notification settings - Fork 0
/
zerosum.java
92 lines (81 loc) · 1.92 KB
/
zerosum.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
ID: 001207j1
LANG: JAVA
TASK: zerosum
*/
import java.io.*;
public class zerosum {
public static int rcd = 0;
public static String[] symbol = { " ", "+", "-" };
public static void main(String[] args) throws IOException {
zerosum z = new zerosum();
z.run();
}
public static void run() throws IOException {
BufferedReader f = new BufferedReader(new FileReader("zerosum.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("zerosum.out")));
int n = Integer.parseInt(f.readLine());
comb(out, 2, n, "");
f.close();
out.close();
}
public static void comb(PrintWriter out, int i, int n, String t) {
if (i > n) {
if (check(t, n))
out.println(p(t, n));
return;
}
for (int j = 0; j < 3; j++) {
String t1 = t;
t1 += symbol[j];
comb(out, i + 1, n, t1);
}
}
public static boolean check(String t, int n) {
int[] rcd = new int[n + 1];
rcd[1] = 1;
int temp = -1;
for (int i = 2; i <= n; i++) {// t.l = n-1;
if (temp != -1 && t.charAt(i - 2) != ' ') {
int l = ("" + temp).length();
if (i > l + 1 && t.charAt(i - l - 2) == '-')
rcd[i - 1] = rcd[i - l - 1] - temp;
else
rcd[i - 1] = rcd[i - l - 1] + temp;
temp = -1;
}
if (t.charAt(i - 2) == ' ') {
if (temp != -1) {
temp = temp * 10 + i;
} else {
temp = (i - 1) * 10 + i;
}
} else if (t.charAt(i - 2) == '+') {
temp = -1;
rcd[i] = rcd[i - 1] + i;
} else if (t.charAt(i - 2) == '-') {
temp = -1;
rcd[i] = rcd[i - 1] - i;
}
}
if (temp != -1) {
int l = ("" + temp).length();
if (n >= l + 1 && t.charAt(n - l - 1) == '-')
rcd[n] = rcd[n - l] - temp;
else
rcd[n] = rcd[n - l] + temp;
temp = -1;
}
System.out.println(rcd[n]);
if (rcd[n] == 0)
return true;
return false;
}
public static String p(String t, int n) {
String fin = "1";
for (int i = 2; i <= n; i++) {
fin += t.charAt(i - 2) + "" + i;
}
return fin;
}
}