Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. int knapsack(int wartosci[], int wagi[], int W, int number) {
  2.  
  3. int N = wagi.length;
  4. int[][] V = new int[N + 1][W + 1];
  5.  
  6. for (int col = 0; col <= W; col++) {
  7. V[0][col] = 0;
  8. }
  9.  
  10. for (int row = 0; row <= N; row++) {
  11. V[row][0] = 0;
  12. }
  13. for (int item=1;item<=N;item++)
  14. {
  15. for (int weight=1;weight<=W;weight++)
  16. {
  17. if (wagi[item-1]<=weight)
  18. {
  19. V[item][weight]=Math.max (wartosci[item-1]+V[item-1][weight-wagi[item-1]], V[item-1][weight]);
  20. }
  21. else
  22. {
  23. V[item][weight]=V[item-1][weight];
  24. }
  25. }
  26. }
  27. return V[N][W];
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement