Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. package asd;
  2.  
  3. /**
  4. *
  5. * @author Lukasz
  6. */
  7. public class plecakowyZachlanny {
  8.  
  9. final static int N = 6;
  10. final static int MAX_V = 10;
  11. final static int[] V = {6, 2, 3, 2, 3, 1};
  12. final static int[] W = {6, 4, 5, 7, 10, 2};
  13.  
  14. public static void odNajcenniejszego() {
  15. boolean[] rozw = new boolean[N];
  16.  
  17. int sumW = 0;
  18. int sumV = 0;
  19. while (true) {
  20. int maxW = 0;
  21.  
  22. int maxPoz =-1;
  23. for (int i = 0; i < N; i++)
  24. {
  25. if (!rozw[i]){
  26. if ((sumV + V[i] <= MAX_V) && (W[i] > maxW)) {
  27. maxW = W[i];
  28. maxPoz = i;
  29. }
  30. }
  31. }
  32. if (maxPoz >-1){
  33. System.out.println("Wzięto: "+(maxPoz+1));
  34. rozw[maxPoz] = true;
  35. sumW = sumW + W[maxPoz];
  36. sumV = sumV+ V[maxPoz];
  37. }else{
  38. break;
  39. }
  40. }
  41. System.out.println("Wartosc optymalnie zapakowanego plecaka: " + sumW);
  42. System.out.print("Przedmioty w plecaku: ");
  43. for (int i = 0; i < N; i++) {
  44. if (rozw[i]) {
  45. System.out.print(i + " ");
  46. }
  47. }
  48. System.out.println();
  49. }
  50. public static void main(String[] args) {
  51. odNajcenniejszego();
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement