aboutsummaryrefslogtreecommitdiff
path: root/Practical2/src/nl/camilstaps
diff options
context:
space:
mode:
authorCamil Staps2015-12-09 23:27:53 +0000
committerCamil Staps2015-12-09 23:27:53 +0000
commit0d4180ab0a80ea854f28b7debff22548ce5974a5 (patch)
tree4daff8a177d89f93d4b6377f6e3b047ab0469be8 /Practical2/src/nl/camilstaps
parentWorking solution Practical 2, small fixes (diff)
Removed Java Practical2; moved C to main directory
Diffstat (limited to 'Practical2/src/nl/camilstaps')
-rw-r--r--Practical2/src/nl/camilstaps/cs/CheckoutHelper.java67
-rw-r--r--Practical2/src/nl/camilstaps/cs/Main.java23
2 files changed, 0 insertions, 90 deletions
diff --git a/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java b/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
deleted file mode 100644
index 5688cf0..0000000
--- a/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package nl.camilstaps.cs;
-
-import java.util.ArrayList;
-
-/**
- * Created by camil on 11/28/15.
- */
-class CheckoutHelper {
- private final ArrayList<Integer> products = new ArrayList<>();
- private int minimal_price;
-
- 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 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) * 5;
- int rest = price % 5;
-
- minimal_price += has_to_pay;
- if (rest != 0) {
- products.add(rest);
- }
- }
-
-}
diff --git a/Practical2/src/nl/camilstaps/cs/Main.java b/Practical2/src/nl/camilstaps/cs/Main.java
deleted file mode 100644
index 2009c4d..0000000
--- a/Practical2/src/nl/camilstaps/cs/Main.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package nl.camilstaps.cs;
-
-import java.util.Scanner;
-
-public class Main {
-
- public static void main(String[] args) {
- CheckoutHelper ch = new CheckoutHelper();
- int n_dividers = readCheckoutHelper(new Scanner(System.in), ch);
- System.out.println(ch.minimumPayment(n_dividers));
- }
-
- private static int readCheckoutHelper(Scanner sc, CheckoutHelper ch) {
- int n_products = sc.nextInt();
- int n_dividers = sc.nextInt();
-
- for (int i = 0; i < n_products; i++)
- ch.addProduct(sc.nextInt());
-
- return n_dividers;
- }
-
-}