Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asd;
- /**
- *
- * @author Lukasz
- */
- public class plecakowyZachlanny {
- final static int N = 6;
- final static int MAX_V = 10;
- final static int[] V = {6, 2, 3, 2, 3, 1};
- final static int[] W = {6, 4, 5, 7, 10, 2};
- public static void odNajcenniejszego() {
- boolean[] rozw = new boolean[N];
- int sumW = 0;
- int sumV = 0;
- while (true) {
- int maxW = 0;
- int maxPoz =-1;
- for (int i = 0; i < N; i++)
- {
- if (!rozw[i]){
- if ((sumV + V[i] <= MAX_V) && (W[i] > maxW)) {
- maxW = W[i];
- maxPoz = i;
- }
- }
- }
- if (maxPoz >-1){
- System.out.println("Wzięto: "+(maxPoz+1));
- rozw[maxPoz] = true;
- sumW = sumW + W[maxPoz];
- sumV = sumV+ V[maxPoz];
- }else{
- break;
- }
- }
- System.out.println("Wartosc optymalnie zapakowanego plecaka: " + sumW);
- System.out.print("Przedmioty w plecaku: ");
- for (int i = 0; i < N; i++) {
- if (rozw[i]) {
- System.out.print(i + " ");
- }
- }
- System.out.println();
- }
- public static void main(String[] args) {
- odNajcenniejszego();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement