andkamen

problem 2

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