/* * 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 kolosalgorytmy; import java.util.Random; public class PlecakDecyzyjnyMC1 { /** * @param args the command line arguments */ static final int MAX_V = 10; static final int[] V = {6, 2, 3, 2, 3, 1}; static final int[] W = {6, 4, 5, 7, 10, 2}; static final Random losuj = new Random(); static final int LICZBA_LOSOWAN = 10; public static void main(String[] args) { // TODO code application logic here boolean[] maxPrzedmioty = new boolean[V.length]; int maxWartosc = 0; for (int i = 0; i < LICZBA_LOSOWAN; i++) { boolean[] przedmioty = losujPrzedmioty(); if (wartosc(przedmioty) > maxWartosc) { maxWartosc = wartosc(przedmioty); maxPrzedmioty = przedmioty; } } System.out.println("Wartosc: " + maxWartosc); System.out.println("Objętość: " + objetosc(maxPrzedmioty)); System.out.print("Przedmioty: "); for (int i = 0; i < maxPrzedmioty.length; i++) { if(maxPrzedmioty[i] == true){ System.out.print(i + " "); } } } static boolean[] losujPrzedmioty() { boolean[] przedmioty = new boolean[V.length]; int i = losuj.nextInt(V.length); int v = V[i]; while (v <= MAX_V) { przedmioty[i] = true; i = losuj.nextInt(V.length); if (!przedmioty[i]) { v = v + V[i]; } } return przedmioty; } static int wartosc(boolean[] przedmioty) { int w = 0; for (int i = 0; i < W.length; i++) { if (przedmioty[i] == true) { w = w + W[i]; } } return w; } static int objetosc(boolean[] przedmioty) { int v = 0; for (int i = 0; i < V.length; i++) { if (przedmioty[i] == true) { v = v + V[i]; } } return v; } }