-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzerosum.cpp
65 lines (61 loc) · 1.4 KB
/
zerosum.cpp
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
#include <bits/stdc++.h>
using namespace std;
bool chkzerosum(int N, vector<int> ops){
int currn = 0;
vector<int> parsedops;
for(int i = 1; i < N; i++){
currn *= 10;
currn += i;
if(ops[i-1] > 0){
parsedops.push_back(currn);
currn = 0;
}
}
currn *= 10;
currn += N;
parsedops.push_back(currn);
int sum = parsedops[0];
int opidx = 0;
for(int i = 1; i < parsedops.size(); i++){
while(ops[opidx] == 0){
opidx++;
}
int mp = 1;
if(ops[opidx] == 2){
mp = -1;
}
sum += mp * parsedops[i];
opidx++;
}
if(sum == 0)
return true;
return false;
}
void dfs(int N, vector<int> ops){
if(ops.size() == N-1){
if(chkzerosum(N, ops)){
for(int i = 0; i < N-1; i++){
cout << i + 1;
if(ops[i] == 0) cout << " ";
else if(ops[i] == 1) cout << "+";
else cout << "-";
}
cout << N << endl;
}
}else{
for(int i = 0; i < 3; i++){
vector<int> nxtops = ops;
nxtops.push_back(i);
dfs(N, nxtops);
}
}
}
int main(){
freopen("zerosum.in", "r", stdin);
freopen("zerosum.out", "w", stdout);
int N; cin >> N;
vector<int> empt;
dfs(N, empt);
vector<int> test;
return 0;
}