Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package do_semestralka;
  7.  
  8. import java.io.FileNotFoundException;
  9. import java.io.FileReader;
  10. import java.io.PrintWriter;
  11. import java.util.Scanner;
  12.  
  13. /**
  14. *
  15. * @author Werto
  16. */
  17. public class Batoh {
  18. private int n; //pocet predmetov
  19. private int K; //maximalna kapacita batohu
  20. private int r; //maximalny pocet predmetov
  21. private int[] a; //vahy predmetov
  22. private int[] c; //ceny predmetov
  23. private boolean[] x; //umiestnene predmety
  24. private int hmotnost; //hmotnost umiestnenych predmetov
  25. private int cena; //cena umiestnenych predmetov
  26. private int pocet; //pocet umiestneny predmetov
  27.  
  28. public Batoh(int n,int K,int r){
  29. this.n = n;
  30. this.K = K;
  31. this.r = r;
  32. a = new int[n];
  33. c = new int[n];
  34. x = new boolean[n];
  35. hmotnost = 0;
  36. cena = 0;
  37. pocet = 0;
  38. }
  39.  
  40. public void nacitajVahuZoSuboru(String nazovSuboru) throws FileNotFoundException {
  41. Scanner scan = new Scanner(new FileReader(nazovSuboru));
  42. for (int i = 0; i < n; i++) {
  43. a[i]=scan.nextInt(); //nacitanie vahy predmetov
  44. }
  45. scan.close();
  46. }
  47.  
  48. public void nacitajCenuZoSuboru(String nazovSuboru) throws FileNotFoundException {
  49. Scanner scan = new Scanner(new FileReader(nazovSuboru));
  50. for (int i = 0; i < n; i++) {
  51. c[i]=scan.nextInt(); //nacitanie ceny predmetov
  52. }
  53. scan.close();
  54. }
  55.  
  56. public void vychodiskoveRiesenie(){
  57. for (int i = 0; i < n; i++) {
  58. x[i] = false;
  59.  
  60. }
  61. }
  62.  
  63. public void primarnaVsuvaciaHeur()
  64. {
  65. int pomIndex = 0; // index najlahsieho predmetu
  66. int min = Integer.MAX_VALUE;
  67. while(hmotnost<=K && pocet<=r)
  68. {
  69. for(int i =0;i<n;i++)
  70. {
  71. if(a[i]< min & x[i] == false) // hmotnost vybraneho min
  72. {
  73. pomIndex = i;
  74. min = a[i];
  75. }
  76. }
  77. if(hmotnost + a[pomIndex] > K || pocet+1>r)
  78. {
  79. return;
  80. }
  81. cena += c[pomIndex];
  82. hmotnost += a[pomIndex];
  83. x[pomIndex] = true;
  84. min = Integer.MAX_VALUE;
  85. pomIndex = 0;
  86. pocet++;
  87. }
  88. }
  89.  
  90. public void vymennaHeurNajVhodny(){
  91. int[] za = new int[pocet]; //zaradene
  92. int[] ne = new int[n-pocet]; //nezaradene
  93. int pocZa = 0; //pocet zaradenych
  94. int pocNe = 0; //pocet nezaradenych
  95. int pom = 0;
  96. boolean nastalaZmena = false;
  97.  
  98. for (int i = 0; i < n; i++) {
  99. if(x[i]==true){
  100. za[pocZa] = i;
  101. pocZa++;
  102. }else{
  103. ne[pocNe] = i;
  104. pocNe++;
  105. }
  106. }
  107.  
  108. do{
  109. nastalaZmena = false;
  110. for (int i = 0; i < za.length; i++) {
  111. for (int j = 0; j < ne.length; j++) {
  112. if((c[za[i]]<c[ne[j]]) && (hmotnost-a[za[i]]+a[ne[j]])<=K){
  113. cena = cena - c[za[i]] + c[ne[j]];
  114. hmotnost = hmotnost - a[za[i]] + a[ne[j]];
  115. pom = za[i];
  116. za[i] = ne[j];
  117. ne[j] = pom;
  118. nastalaZmena = true;
  119. }else{
  120. nastalaZmena = false;
  121. }
  122. }
  123. }
  124. }while(nastalaZmena);
  125. }
  126.  
  127. public void ulozBatohDoSuboru(String nazovSuboru) throws FileNotFoundException {
  128. PrintWriter writer = new PrintWriter(nazovSuboru);
  129. writer.println("n = "+n+", K = "+K+", r = "+r);
  130. writer.println("aktual. pocet = "+pocet+", aktual. vaha = "+hmotnost+", aktual. cena = "+cena);
  131. writer.println("Predmet\t Cena\t Vaha\t Umiestnenie ");
  132. for(int i =0;i<n;i++)
  133. {
  134. writer.println((i+1)+"\t"+c[i]+"\t"+a[i]+"\t"+(x[i]?"ano":"nie"));
  135. }
  136. writer.close();
  137. }
  138.  
  139. public String toString()
  140. {
  141. return "Batoh:\nmax. kapacita " + K + "\nmax. pocet predmetov " + r +
  142. "\naktualna hmotnost " + hmotnost + "\naktualna cena " + cena +
  143. "\npocet predmetov v batohu " + pocet;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement