Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- //vector <int> vec{3,6,5,3,6};
- vector <int> vec{2,5,7};
- int result = 0;
- int counter = 0, tempCounter =0;
- int previousValue = 0;
- int KnapSack(int currentPos, int currentCapacity)
- {
- int localCounter =0;
- if(currentCapacity ==0)
- {
- // cout << "current cap return" << endl;
- counter = tempCounter;
- // localCounter = counter;
- // cout << "Local counter: " << endl;
- return counter;
- exit(2);
- }
- if(currentPos>=vec.size())
- {
- //cout << "current pos return" << endl;
- return 0;
- }
- int currentValue = vec.at(currentPos);
- if(currentValue<=currentCapacity)
- {
- currentCapacity = currentCapacity-currentValue;
- currentPos++;
- tempCounter++;
- previousValue = currentValue;
- KnapSack(currentPos,currentCapacity);
- }
- if(currentValue>currentCapacity)
- {
- currentCapacity = currentCapacity + previousValue;
- --tempCounter;
- KnapSack(currentPos,currentCapacity);
- }
- return 0;
- }
- int main(){
- //cout << vec.size() << endl;
- sort(vec.begin(), vec.end());
- int volume = 6;
- int ans = KnapSack(0, volume);
- cout << counter;
- }
Advertisement
Add Comment
Please, Sign In to add comment