aboutsummaryrefslogtreecommitdiff
path: root/Practical2
diff options
context:
space:
mode:
authorCamil Staps2015-11-30 19:49:43 +0100
committerCamil Staps2015-11-30 19:49:43 +0100
commit5c674c7b76ba2ec1951abd39a214f8764e3c0fde (patch)
treed133cc7ae305afb9c47ca3a3ade5dde95686d422 /Practical2
parentfinish assignment 11 (diff)
Practical2 seems to work
Diffstat (limited to 'Practical2')
-rw-r--r--Practical2/src/nl/camilstaps/cs/CheckoutHelper.java45
-rw-r--r--Practical2/tester/samples/test_1.in3
-rw-r--r--Practical2/tester/samples/test_1.out1
3 files changed, 44 insertions, 5 deletions
diff --git a/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java b/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
index 5ad7d2d..5688cf0 100644
--- a/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
+++ b/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
@@ -12,15 +12,50 @@ class CheckoutHelper {
public CheckoutHelper() {
}
+ private static int round(int cents) throws IllegalArgumentException {
+ switch (cents % 5) {
+ case 0: return cents;
+ case 1: return cents - 1;
+ case 2: return cents - 2;
+ case 3: return cents + 2;
+ case 4: return cents + 1;
+ }
+ throw new IllegalArgumentException("Incorrect number of cents.");
+ }
+
public int minimumPayment(int dividers) {
- int sum = minimal_price;
- for (int n : products)
- sum += n;
- return sum;
+ int minimums[][] = new int[products.size() + 1][dividers + 1];
+ int last[][] = new int[products.size() + 1][dividers + 1];
+ for (int p = 0; p <= products.size(); p++) {
+ for (int d = 0; d <= dividers; d++) {
+ if (p == 0) {
+ minimums[p][d] = 0; // No products
+ last[p][d] = 0;
+ } else if (d == 0) {
+ int sum = 0;
+ for (int p2 = 0; p < products.size() && p2 < p; p2++)
+ sum += products.get(p2);
+ minimums[p][d] = round(sum); // no dividers
+ last[p][d] = sum;
+ } else {
+ int try1 = minimums[p-1][d] - round(last[p-1][d]) + round(last[p-1][d] + products.get(p-1)); // don't place a divider
+ int try2 = minimums[p-1][d-1] + round(products.get(p-1)); // place a divider
+ if (try1 < try2) {
+ minimums[p][d] = try1;
+ last[p][d] = last[p-1][d] + products.get(p-1);
+ } else {
+ minimums[p][d] = try2;
+ last[p][d] = products.get(p-1);
+ }
+ }
+ }
+ }
+
+ return minimal_price + minimums[products.size()][dividers];
}
public void addProduct(int price) {
- int has_to_pay = price / 5;
+ int has_to_pay = (price / 5) * 5;
int rest = price % 5;
minimal_price += has_to_pay;
diff --git a/Practical2/tester/samples/test_1.in b/Practical2/tester/samples/test_1.in
new file mode 100644
index 0000000..7e3148c
--- /dev/null
+++ b/Practical2/tester/samples/test_1.in
@@ -0,0 +1,3 @@
+100 60
+1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
+
diff --git a/Practical2/tester/samples/test_1.out b/Practical2/tester/samples/test_1.out
new file mode 100644
index 0000000..d567f8a
--- /dev/null
+++ b/Practical2/tester/samples/test_1.out
@@ -0,0 +1 @@
+4950