193030

D WIP, without input

May 9th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include<fstream>
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. //vector <int> vec{3,6,5,3,6};
  8. vector <int> vec{2,5,7};
  9.  
  10.  
  11. int result = 0;
  12. int counter = 0, tempCounter =0;
  13. int previousValue = 0;
  14. int KnapSack(int currentPos, int currentCapacity)
  15. {
  16. int localCounter =0;
  17. if(currentCapacity ==0)
  18. {
  19. // cout << "current cap return" << endl;
  20. counter = tempCounter;
  21. // localCounter = counter;
  22. // cout << "Local counter: " << endl;
  23. return counter;
  24. exit(2);
  25.  
  26. }
  27. if(currentPos>=vec.size())
  28. {
  29. //cout << "current pos return" << endl;
  30. return 0;
  31. }
  32. int currentValue = vec.at(currentPos);
  33. if(currentValue<=currentCapacity)
  34. {
  35. currentCapacity = currentCapacity-currentValue;
  36. currentPos++;
  37. tempCounter++;
  38. previousValue = currentValue;
  39. KnapSack(currentPos,currentCapacity);
  40. }
  41. if(currentValue>currentCapacity)
  42. {
  43. currentCapacity = currentCapacity + previousValue;
  44. --tempCounter;
  45. KnapSack(currentPos,currentCapacity);
  46. }
  47. return 0;
  48. }
  49.  
  50. int main(){
  51. //cout << vec.size() << endl;
  52. sort(vec.begin(), vec.end());
  53. int volume = 6;
  54. int ans = KnapSack(0, volume);
  55. cout << counter;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment