Advertisement
KolosAISD

PlecakDecBS

Nov 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. /* Decyzyjny Problem Plecakowy bez wypisywnia numerow rzeczy */
  2. /* Metoda brutalnej sily */
  3.  
  4. class PlecakDecBS
  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.  
  11. public static void main(String[] args)
  12. {
  13. int p = 0;
  14. int v,w;
  15. int b1,b2,b3,b4,b5,b6;
  16. int[] tab = new int[N]; // przedmioty w plecaku
  17. for (b1 = 0; b1 <= 1; b1++)
  18. for (b2 = 0; b2 <= 1; b2++)
  19. for (b3 = 0; b3 <= 1; b3++)
  20. for (b4 = 0; b4 <= 1; b4++)
  21. for (b5 = 0; b5 <= 1; b5++)
  22. for (b6 = 0; b6 <= 1; b6++)
  23. {
  24. v = b1*V[0] + b2*V[1] + b3*V[2] + b4*V[3] + b5*V[4] + b6*V[5];
  25. if (v <= MAX_V)
  26. {
  27. w = b1*W[0] + b2*W[1] + b3*W[2] + b4*W[3] + b5*W[4] + b6*W[5];
  28. if (p < w)
  29. {
  30. p = w;
  31. tab[0] = b1; tab[1] = b2; tab[2] = b3;
  32. tab[3] = b4; tab[4] = b5; tab[5] = b6;
  33. }
  34. }
  35. }
  36. System.out.println("Wartosc plecaka: " + p);
  37. System.out.print("Przedmioty w plecaku: ");
  38. for (int i = 0; i < N; i++)
  39. if (tab[i] == 1) System.out.print(i + " ");
  40. System.out.println();
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement