Advertisement
chaosagent

USACO zerosum

Jun 30th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. /*
  2. ID: david.h2
  3. LANG: C++11
  4. TASK: zerosum
  5. */
  6.  
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. FILE *fout = fopen("zerosum.out", "w");
  13.  
  14. int n;
  15. vector<int> currentsequence(8, -1);
  16.  
  17. void test(int left) {
  18.     if (left == 0) {
  19.         vector<int> digits(n, 0);
  20.         digits[0] = 1;
  21.         int i = 0;
  22.         int opi = 0;
  23.         for (int digit = 2; digit <= n; digit++) {
  24.             if (currentsequence[opi] == 0) {
  25.                 digits[i] *= 10;
  26.                 digits[i] += digit;
  27.             } else {
  28.                 i++;
  29.                 digits[i] = digit;
  30.             }
  31.             opi++;
  32.         }
  33.         int w = digits[0];
  34.         int currdigit = 1;
  35.         for (int op = 0; op < n - 1; op++) {
  36.             if (digits[currdigit] == 0) {
  37.                 break;
  38.             }
  39.             if (currentsequence[op] == 0) {
  40.                 continue;
  41.             }
  42.             if (currentsequence[op] == 1) {
  43.                 w += digits[currdigit];
  44.             } else if (currentsequence[op] == 2) {
  45.                 w -= digits[currdigit];
  46.             }
  47.             currdigit++;
  48.         }
  49.         if (w == 0) {
  50.             fprintf(fout, "%d", 1);
  51.             for (int p = 2; p <= n; p++) {
  52.                 if (currentsequence[p - 2] == 0) {
  53.                     fprintf(fout, "%c", ' ');
  54.                 } else if (currentsequence[p - 2] == 1) {
  55.                     fprintf(fout, "%c", '+');
  56.                 } else if (currentsequence[p - 2] == 2) {
  57.                     fprintf(fout, "%c", '-');
  58.                 }
  59.                 fprintf(fout, "%d", p);
  60.             }
  61.             fprintf(fout, "\n");
  62.         }
  63.         return;
  64.     }
  65.     for (int new_op = 0; new_op <= 2; new_op++) {
  66.         currentsequence[n - left - 1] = new_op;
  67.         test(left - 1);
  68.     }
  69. }
  70.  
  71. int main() {
  72.     FILE *fin = fopen("zerosum.in", "r");
  73.     fscanf(fin, "%d", &n);
  74.     test(n - 1);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement