Advertisement
KolosAISD

PlecakDZ

Nov 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1.  
  2. package kolosaisd;
  3.  
  4. class PlecakDZ
  5. {
  6. final static int N = 6; // liczba przedmiotów
  7. final static int MAX_V = 10; // objetość plecaka
  8. final static int[] V = {6,2,3,2,3,1}; // objetości przedmiotów
  9. final static int[] W = {6,4,5,7,10,2}; // wartości przedmiotów
  10. static int P(int i, int v)
  11. {
  12. int w1; // wartość, gdy nie bierzemy i-tego przedmiotu
  13. int w2; // wartość, gdy bierzemy i-ty przedmiot
  14. if (i == 0 && v < V[0]) return 0;
  15. if (i == 0 && v >= V[0]) return W[0];
  16. w1 = P(i-1,v);
  17. if (i > 0 && v < V[i]) return w1;
  18. w2 = W[i] + P(i-1,v-V[i]);
  19. if (w2 > w1) // gdy i > 0 && v >= V[i]
  20. return w2;
  21. else
  22. return w1;
  23. }
  24. public static void main(String[] args)
  25. {
  26. System.out.println("Wartosc przedmiotow: " + P(N-1,MAX_V));
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement