Advertisement
andkamen

2ra final

Jan 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. void printPartitions(int startValue, int target, int maxValue, string suffix) {
  7.     if (target == 0) {
  8.         suffix.erase(0,3);
  9.         cout <<startValue<<" = "<< suffix << endl;
  10.     } else {
  11.         if (maxValue <= target) {
  12.             ostringstream oss;
  13.             oss << suffix << " + " << maxValue;
  14.             string temp = oss.str();
  15.             printPartitions(startValue, target - maxValue, maxValue, temp);
  16.         }
  17.         if (maxValue > 1) {
  18.             printPartitions(startValue, target, maxValue - 1, suffix);
  19.         }
  20.     }
  21. }
  22.  
  23. int main() {
  24.     int n;
  25.     cin >> n;
  26.  
  27.     printPartitions(n, n, n, "");
  28.  
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement