Advertisement
STANAANDREY

perms sum

Jul 28th, 2020
1,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define newline '\n'
  3. using namespace std;
  4. ifstream fin("text.in");
  5. ofstream fout("text.out");
  6. ///**********************
  7. int n;
  8. int st[50];
  9. bool used[50];
  10.  
  11. void display(int top) {
  12.     for (int i = 1; i < top; i++)
  13.         fout << st[i] << ' ';
  14.     fout << newline;
  15. }
  16.  
  17. void backTr(int top, int sum) {
  18.     if (sum >= n) {
  19.         if (sum == n)
  20.             display(top);
  21.         return;
  22.     }
  23.  
  24.     for (int i = 1; i <= n; i++)
  25.         if (!used[i]) {
  26.             st[top] = i;
  27.             used[i] = true;
  28.             backTr(top + 1, sum + i);
  29.             used[i] = false;
  30.         }
  31. }
  32.  
  33. int main() {
  34.     fin >> n;
  35.     backTr(1, 0);
  36.     fout.close();
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement