Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: david.h2
- LANG: C++11
- TASK: zerosum
- */
- #include <iostream>
- #include <vector>
- using namespace std;
- FILE *fout = fopen("zerosum.out", "w");
- int n;
- vector<int> currentsequence(8, -1);
- void test(int left) {
- if (left == 0) {
- vector<int> digits(n, 0);
- digits[0] = 1;
- int i = 0;
- int opi = 0;
- for (int digit = 2; digit <= n; digit++) {
- if (currentsequence[opi] == 0) {
- digits[i] *= 10;
- digits[i] += digit;
- } else {
- i++;
- digits[i] = digit;
- }
- opi++;
- }
- int w = digits[0];
- int currdigit = 1;
- for (int op = 0; op < n - 1; op++) {
- if (digits[currdigit] == 0) {
- break;
- }
- if (currentsequence[op] == 0) {
- continue;
- }
- if (currentsequence[op] == 1) {
- w += digits[currdigit];
- } else if (currentsequence[op] == 2) {
- w -= digits[currdigit];
- }
- currdigit++;
- }
- if (w == 0) {
- fprintf(fout, "%d", 1);
- for (int p = 2; p <= n; p++) {
- if (currentsequence[p - 2] == 0) {
- fprintf(fout, "%c", ' ');
- } else if (currentsequence[p - 2] == 1) {
- fprintf(fout, "%c", '+');
- } else if (currentsequence[p - 2] == 2) {
- fprintf(fout, "%c", '-');
- }
- fprintf(fout, "%d", p);
- }
- fprintf(fout, "\n");
- }
- return;
- }
- for (int new_op = 0; new_op <= 2; new_op++) {
- currentsequence[n - left - 1] = new_op;
- test(left - 1);
- }
- }
- int main() {
- FILE *fin = fopen("zerosum.in", "r");
- fscanf(fin, "%d", &n);
- test(n - 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement