Guest User

Untitled

a guest
Oct 30th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. //this function just prints the stack
  7. void printStack(stack<int> Q) {
  8.     while (!Q.empty()) {
  9.         cout << Q.top() << " ";
  10.         Q.pop();
  11.     }
  12.     cout << endl;
  13. }
  14.  
  15. void rec (int C, int S[], int N, stack<int> Q) {
  16.     if (N==-1) {
  17.         printStack(Q);
  18.         return;
  19.     }
  20.     //try all possible values for the current coordinate
  21.     for (int i=0;i<=C/S[N];i++) {
  22.         Q.push(i);
  23.         //remaining capacity is C-i*S[N]
  24.         rec (C-i*S[N], S, N-1, Q);
  25.         Q.pop();
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.    int C = 35; //this is the Capacity
  32.    int N = 2;  //your N
  33.    stack <int> Q; //the stack where the program stores the vectors to be printed
  34.    int S[N];  //the array of the weights
  35.    
  36.    //the values of the weights
  37.    S[0] = 3;
  38.    S[1] = 4;
  39.    rec(C,S,N-1,Q);
  40.    
  41.    return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment