From 454b46137acc65837a2569dd043548257225c1cb Mon Sep 17 00:00:00 2001 From: Maksim Date: Sun, 6 Oct 2019 10:50:30 +1000 Subject: [PATCH 1/2] add .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ec4f15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +!*.java + From f3f13c0b663281eb00bdfe595631e6ee64e04ace Mon Sep 17 00:00:00 2001 From: Maksim Date: Wed, 16 Oct 2019 19:28:54 +1000 Subject: [PATCH 2/2] recursion pow and bag problem solution --- recursion/src/Recursion.java | 72 +++++++++++++++++++++ recursion/src/fill_bag/Bag.java | 43 ++++++++++++ recursion/src/fill_bag/Thing.java | 15 +++++ recursion/src/pow/NegativePowException.java | 7 ++ recursion/src/pow/Pow.java | 18 ++++++ 5 files changed, 155 insertions(+) create mode 100644 recursion/src/Recursion.java create mode 100644 recursion/src/fill_bag/Bag.java create mode 100644 recursion/src/fill_bag/Thing.java create mode 100644 recursion/src/pow/NegativePowException.java create mode 100644 recursion/src/pow/Pow.java diff --git a/recursion/src/Recursion.java b/recursion/src/Recursion.java new file mode 100644 index 0000000..7b52574 --- /dev/null +++ b/recursion/src/Recursion.java @@ -0,0 +1,72 @@ +import fill_bag.Bag; +import fill_bag.Thing; +import pow.NegativePowException; +import pow.Pow; + +public class Recursion { + public static void main(String[] args) { + // Test pow() + System.out.println("------ Test recursion pow ------"); + System.out.println(); + + int x = 3; + int n1 = -3, n2 = 0, n3 = 3; + + try { + System.out.println(String.format("pow(%d, %d) = %d", x, n1, Pow.pow(x, n1))); + } catch (NegativePowException e) { + System.out.println(e.getMessage()); + } + + try { + System.out.println(String.format("pow(%d, %d) = %d", x, n2, Pow.pow(x, n2))); + } catch (NegativePowException e) { + System.out.println(e.getMessage()); + } + + try { + System.out.println(String.format("pow(%d, %d) = %d", x, n3, Pow.pow(x, n3))); + } catch (NegativePowException e) { + System.out.println(e.getMessage()); + } + + System.out.println(); + + // Test placement() + System.out.println("------ Test recursion pow ------"); + System.out.println(); + + Bag bag = new Bag(14); + + Thing[] things = new Thing[]{ + new Thing(5, 3), + new Thing(10, 5), + new Thing(6, 4), + new Thing(5, 2) + }; + + bag.fill(things); + System.out.println(bag); + + + Bag bag1 = new Bag(3); + + bag1.fill(things); + System.out.println(bag1); + + Bag bag2 = new Bag(5); + + bag2.fill(things); + System.out.println(bag2); + + Bag bag3 = new Bag(10); + + bag3.fill(things); + System.out.println(bag3); + + } +} + + + + diff --git a/recursion/src/fill_bag/Bag.java b/recursion/src/fill_bag/Bag.java new file mode 100644 index 0000000..9707f52 --- /dev/null +++ b/recursion/src/fill_bag/Bag.java @@ -0,0 +1,43 @@ +package fill_bag; + +public class Bag { + public int capacity; + + public int cost; + public int weight; + public int numThings; + + private int sumWeight; + private int sumCost; + private int count; + + public Bag(int _cap) { + capacity = _cap; + } + + public void fill(Thing[] things) { + combination0(things, 0, things.length, 0, 0, 0); + } + + private void combination0(Thing[] arr, int start, int end, int count, int sumWeight, int sumCost) { + if (capacity >= sumWeight) { + if (sumCost > cost) { + cost = sumCost; + weight = sumWeight; + numThings = count; + } + } + + if (start == end) { + return; + } + + for (int i = start; i < end; i++) { + combination0(arr, i + 1, end, count+1, sumWeight + arr[i].weight, sumCost + arr[i].cost); + } + } + + public String toString() { + return "maxCost = " + cost + ", maxWeight = " + weight + ", things = " + numThings; + } +} diff --git a/recursion/src/fill_bag/Thing.java b/recursion/src/fill_bag/Thing.java new file mode 100644 index 0000000..9f12250 --- /dev/null +++ b/recursion/src/fill_bag/Thing.java @@ -0,0 +1,15 @@ +package fill_bag; + +public class Thing { + public int weight = 0; + public int cost = 0; + + public Thing(int _weight, int _cost) { + weight = _weight; + cost = _cost; + } + + public String toString() { + return "weight: " + weight + ", cost: " + cost; + } +} diff --git a/recursion/src/pow/NegativePowException.java b/recursion/src/pow/NegativePowException.java new file mode 100644 index 0000000..d0d843a --- /dev/null +++ b/recursion/src/pow/NegativePowException.java @@ -0,0 +1,7 @@ +package pow; + +public class NegativePowException extends Exception { + NegativePowException(String msg) { + super(msg); + } +} diff --git a/recursion/src/pow/Pow.java b/recursion/src/pow/Pow.java new file mode 100644 index 0000000..b6e2717 --- /dev/null +++ b/recursion/src/pow/Pow.java @@ -0,0 +1,18 @@ +package pow; + +public class Pow { + public static long pow(int x, int n) throws NegativePowException { + if (n < 0) { + throw new NegativePowException("ERROR: Negative power"); + } + + if (n == 0) { + return 1; + } + else { + return pow(x, n - 1) * x; + } + } +} + +