Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- //this function just prints the stack
- void printStack(stack<int> Q) {
- while (!Q.empty()) {
- cout << Q.top() << " ";
- Q.pop();
- }
- cout << endl;
- }
- void rec (int C, int S[], int N, stack<int> Q) {
- if (N==-1) {
- printStack(Q);
- return;
- }
- //try all possible values for the current coordinate
- for (int i=0;i<=C/S[N];i++) {
- Q.push(i);
- //remaining capacity is C-i*S[N]
- rec (C-i*S[N], S, N-1, Q);
- Q.pop();
- }
- }
- int main()
- {
- int C = 35; //this is the Capacity
- int N = 2; //your N
- stack <int> Q; //the stack where the program stores the vectors to be printed
- int S[N]; //the array of the weights
- //the values of the weights
- S[0] = 3;
- S[1] = 4;
- rec(C,S,N-1,Q);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment