diff --git a/Java4L5HW/src/alexrm84/Backpack.java b/Java4L5HW/src/alexrm84/Backpack.java new file mode 100644 index 0000000..d0826ec --- /dev/null +++ b/Java4L5HW/src/alexrm84/Backpack.java @@ -0,0 +1,69 @@ +package alexrm84; + +import java.util.HashSet; + +public class Backpack { + + private final int MAX_WEIGHT=20; + private int maxPrice=0; + + private HashSet items =new HashSet<>(); + private HashSet resultItems = new HashSet<>(); + + + public static void main(String[] args) { + Backpack backpack =new Backpack(); + backpack.start(); + backpack.display(); + } + + public void start(){ + items.add(new Item("Ручка", 5, 1)); + items.add(new Item("Карандаш", 4, 1)); + items.add(new Item("Учебник", 20, 10)); + items.add(new Item("Тетрадь", 10, 5)); + items.add(new Item("Обед", 15, 15)); + selectionOfItemsSimple(items); + } + + private int getCurrentWeight(HashSet newItems){ + int temp=0; + for (Item item:newItems) { + temp+=item.getWeight(); + } + return temp; + } + + private int getCurrentPrice(HashSet newItems){ + int temp=0; + for (Item item:newItems) { + temp+=item.getPrice(); + } + return temp; + } + + public void selectionOfItemsSimple(HashSet newItems){ + if (newItems.size()==0){ + return; + } + if (getCurrentWeight(newItems)<=MAX_WEIGHT){ + int check=getCurrentPrice(newItems); + if (check>=maxPrice){ + maxPrice=check; + resultItems=newItems; + } + } + for (Item item:newItems) { + HashSet newSet =new HashSet<>(newItems); + newSet.remove(item); + selectionOfItemsSimple(newSet); + } + } + + private void display(){ + System.out.println("Оптимальный состав предметов дающий "+maxPrice+" состоит из:"); + for (Item item:resultItems) { + System.out.print(item.getName()+" "); + } + } +} diff --git a/Java4L5HW/src/alexrm84/Exponentiation.java b/Java4L5HW/src/alexrm84/Exponentiation.java new file mode 100644 index 0000000..8808cc0 --- /dev/null +++ b/Java4L5HW/src/alexrm84/Exponentiation.java @@ -0,0 +1,20 @@ +package alexrm84; + +public class Exponentiation { + + public static void main(String[] args) { + System.out.println(getResult(5,3)); + System.out.println(getResult(5,0)); + System.out.println(getResult(5,-3)); + } + + public static double getResult(int number,int degree){ + if (degree==0){ + return 1; + } + if (degree<0){ + return 1/getResult(number,Math.abs(degree)); + } + return number*getResult(number,degree-1); + } +} diff --git a/Java4L5HW/src/alexrm84/Item.java b/Java4L5HW/src/alexrm84/Item.java new file mode 100644 index 0000000..c6dd871 --- /dev/null +++ b/Java4L5HW/src/alexrm84/Item.java @@ -0,0 +1,25 @@ +package alexrm84; + +public class Item { + private int price; + private int weight; + private String name; + + public Item(String name, int price, int weight) { + this.price = price; + this.weight = weight; + this.name=name; + } + + public int getPrice() { + return price; + } + + public int getWeight() { + return weight; + } + + public String getName() { + return name; + } +}