Advertisement
Okorosso

Untitled

Mar 25th, 2021
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. void gen(int n, int sum, vector<int> a, vector<int> b, vector<vector<int>> &answ) {
  9.  
  10.     if (n == sum) {
  11.         b = a;
  12.         sort(b.begin(), b.end());
  13.         reverse(b.begin(), b.end());
  14.         answ.push_back(b);
  15.  
  16.     }
  17.     int na = 1;
  18.     if (sum != 0) {
  19.         na = a.back();
  20.     }
  21.     for (int i = na; i <= n - sum; i++) {
  22.         a.push_back(i);
  23.         gen(n, sum + i, a, b, answ);
  24.         a.pop_back();
  25.     }
  26. }
  27.  
  28. int main() {
  29.  
  30.     int n;
  31.     vector<vector<int>> answ;
  32.     vector<int> a, b;
  33.  
  34.     ifstream in("input.txt");
  35.     ofstream out("output.txt");
  36.     in >> n;
  37.     gen(n, 0, a, b, answ);
  38.  
  39.     sort(answ.rbegin(), answ.rend());
  40.     reverse(answ.begin(), answ.end());
  41.  
  42.     for (int j = 0; j < answ.size(); j++) {
  43.         for (int i = 0; i < answ[j].size(); i++) {
  44.             out << answ[j][i] << ' ';
  45.         }
  46.         out << '\n';
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement