Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package do_semestralka;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.PrintWriter;
- import java.util.Scanner;
- /**
- *
- * @author Werto
- */
- public class Batoh {
- private int n; //pocet predmetov
- private int K; //maximalna kapacita batohu
- private int r; //maximalny pocet predmetov
- private int[] a; //vahy predmetov
- private int[] c; //ceny predmetov
- private boolean[] x; //umiestnene predmety
- private int hmotnost; //hmotnost umiestnenych predmetov
- private int cena; //cena umiestnenych predmetov
- private int pocet; //pocet umiestneny predmetov
- public Batoh(int n,int K,int r){
- this.n = n;
- this.K = K;
- this.r = r;
- a = new int[n];
- c = new int[n];
- x = new boolean[n];
- hmotnost = 0;
- cena = 0;
- pocet = 0;
- }
- public void nacitajVahuZoSuboru(String nazovSuboru) throws FileNotFoundException {
- Scanner scan = new Scanner(new FileReader(nazovSuboru));
- for (int i = 0; i < n; i++) {
- a[i]=scan.nextInt(); //nacitanie vahy predmetov
- }
- scan.close();
- }
- public void nacitajCenuZoSuboru(String nazovSuboru) throws FileNotFoundException {
- Scanner scan = new Scanner(new FileReader(nazovSuboru));
- for (int i = 0; i < n; i++) {
- c[i]=scan.nextInt(); //nacitanie ceny predmetov
- }
- scan.close();
- }
- public void vychodiskoveRiesenie(){
- for (int i = 0; i < n; i++) {
- x[i] = false;
- }
- }
- public void primarnaVsuvaciaHeur()
- {
- int pomIndex = 0; // index najlahsieho predmetu
- int min = Integer.MAX_VALUE;
- while(hmotnost<=K && pocet<=r)
- {
- for(int i =0;i<n;i++)
- {
- if(a[i]< min & x[i] == false) // hmotnost vybraneho min
- {
- pomIndex = i;
- min = a[i];
- }
- }
- if(hmotnost + a[pomIndex] > K || pocet+1>r)
- {
- return;
- }
- cena += c[pomIndex];
- hmotnost += a[pomIndex];
- x[pomIndex] = true;
- min = Integer.MAX_VALUE;
- pomIndex = 0;
- pocet++;
- }
- }
- public void vymennaHeurNajVhodny(){
- int[] za = new int[pocet]; //zaradene
- int[] ne = new int[n-pocet]; //nezaradene
- int pocZa = 0; //pocet zaradenych
- int pocNe = 0; //pocet nezaradenych
- int pom = 0;
- boolean nastalaZmena = false;
- for (int i = 0; i < n; i++) {
- if(x[i]==true){
- za[pocZa] = i;
- pocZa++;
- }else{
- ne[pocNe] = i;
- pocNe++;
- }
- }
- do{
- nastalaZmena = false;
- for (int i = 0; i < za.length; i++) {
- for (int j = 0; j < ne.length; j++) {
- if((c[za[i]]<c[ne[j]]) && (hmotnost-a[za[i]]+a[ne[j]])<=K){
- cena = cena - c[za[i]] + c[ne[j]];
- hmotnost = hmotnost - a[za[i]] + a[ne[j]];
- pom = za[i];
- za[i] = ne[j];
- ne[j] = pom;
- nastalaZmena = true;
- }else{
- nastalaZmena = false;
- }
- }
- }
- }while(nastalaZmena);
- }
- public void ulozBatohDoSuboru(String nazovSuboru) throws FileNotFoundException {
- PrintWriter writer = new PrintWriter(nazovSuboru);
- writer.println("n = "+n+", K = "+K+", r = "+r);
- writer.println("aktual. pocet = "+pocet+", aktual. vaha = "+hmotnost+", aktual. cena = "+cena);
- writer.println("Predmet\t Cena\t Vaha\t Umiestnenie ");
- for(int i =0;i<n;i++)
- {
- writer.println((i+1)+"\t"+c[i]+"\t"+a[i]+"\t"+(x[i]?"ano":"nie"));
- }
- writer.close();
- }
- public String toString()
- {
- return "Batoh:\nmax. kapacita " + K + "\nmax. pocet predmetov " + r +
- "\naktualna hmotnost " + hmotnost + "\naktualna cena " + cena +
- "\npocet predmetov v batohu " + pocet;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement