Advertisement
andkamen

2ra

Jan 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 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.         cout <<startValue<<" = "<< suffix << endl;
  9.     } else {
  10.         if (maxValue <= target) {
  11.             ostringstream oss;
  12.             oss << maxValue << " + " << suffix;
  13.             string temp = oss.str();
  14.             printPartitions(startValue, target - maxValue, maxValue, temp);
  15.         }
  16.         if (maxValue > 1) {
  17.             printPartitions(startValue, target, maxValue - 1, suffix);
  18.         }
  19.     }
  20. }
  21.  
  22. int main() {
  23.     int n;
  24.     cin >> n;
  25.  
  26.     printPartitions(n, n, n, "");
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement