From 0d4180ab0a80ea854f28b7debff22548ce5974a5 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 9 Dec 2015 23:27:53 +0000 Subject: Removed Java Practical2; moved C to main directory --- Practical2/Makefile | 13 ++++ Practical2/c/Makefile | 13 ---- Practical2/c/checkout.c | 73 ---------------------- Practical2/checkout.c | 73 ++++++++++++++++++++++ .../src/nl/camilstaps/cs/CheckoutHelper.java | 67 -------------------- Practical2/src/nl/camilstaps/cs/Main.java | 23 ------- Practical2/tester/test.sh | 12 +--- 7 files changed, 88 insertions(+), 186 deletions(-) create mode 100644 Practical2/Makefile delete mode 100644 Practical2/c/Makefile delete mode 100644 Practical2/c/checkout.c create mode 100644 Practical2/checkout.c delete mode 100644 Practical2/src/nl/camilstaps/cs/CheckoutHelper.java delete mode 100644 Practical2/src/nl/camilstaps/cs/Main.java (limited to 'Practical2') diff --git a/Practical2/Makefile b/Practical2/Makefile new file mode 100644 index 0000000..e64a1f7 --- /dev/null +++ b/Practical2/Makefile @@ -0,0 +1,13 @@ +CC=gcc +OBJS=checkout + +.PHONY: all run clean + +all: $(OBJS) + +$(OBJS): % : %.c + $(CC) -o $@ $< + +clean: + rm $(OBJS) + diff --git a/Practical2/c/Makefile b/Practical2/c/Makefile deleted file mode 100644 index e64a1f7..0000000 --- a/Practical2/c/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -CC=gcc -OBJS=checkout - -.PHONY: all run clean - -all: $(OBJS) - -$(OBJS): % : %.c - $(CC) -o $@ $< - -clean: - rm $(OBJS) - diff --git a/Practical2/c/checkout.c b/Practical2/c/checkout.c deleted file mode 100644 index 82eda3a..0000000 --- a/Practical2/c/checkout.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include - -uint32_t round_ct(uint32_t cents) { - 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; - } -} - -int32_t maximum_profit(uint16_t products[], uint16_t n_products, uint8_t dividers) { - uint32_t sums[n_products+1][n_products+1]; - uint16_t i, j; - for (i = 0; i <= n_products; i++) { - for (j = 0; j <= n_products; j++) { - if (j <= i) { - sums[i][j] = 0; - } else if (j == 1) { - sums[i][j] = products[0]; - } else { - sums[i][j] = sums[i][j-1] + products[j-1]; - } - } - } - - int32_t profit[n_products+1][dividers+1]; - uint16_t p, d; - for (p = 0; p <= n_products; p++) { - for (d = 0; d <= dividers; d++) { - if (p == 0) { - profit[p][d] = 0; - } else if (d == 0) { - profit[p][d] = sums[0][p] - round_ct(sums[0][p]); - } else { - int32_t max = profit[p][d-1]; - uint16_t i; - for (i = 0; i < p; i++) { - int32_t try = profit[i][d-1] + sums[i][p] - round_ct(sums[i][p]); - if (try > max) - max = try; - } - profit[p][d] = max; - } - } - } - - return profit[n_products][dividers]; -} - -int main(void) { - int n_products; - int dividers; - uint32_t sum = 0; - - scanf("%d %d", &n_products, ÷rs); - - uint16_t products[n_products]; - uint16_t i; - for (i = 0; i < n_products; i++) { - scanf("%d", &(products[i])); - sum += products[i]; - if (products[i] % 5 == 0) { - n_products--; - i--; - } - } - - printf("%d\n", sum - maximum_profit(products, n_products, dividers)); -} - diff --git a/Practical2/checkout.c b/Practical2/checkout.c new file mode 100644 index 0000000..82eda3a --- /dev/null +++ b/Practical2/checkout.c @@ -0,0 +1,73 @@ +#include +#include + +uint32_t round_ct(uint32_t cents) { + 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; + } +} + +int32_t maximum_profit(uint16_t products[], uint16_t n_products, uint8_t dividers) { + uint32_t sums[n_products+1][n_products+1]; + uint16_t i, j; + for (i = 0; i <= n_products; i++) { + for (j = 0; j <= n_products; j++) { + if (j <= i) { + sums[i][j] = 0; + } else if (j == 1) { + sums[i][j] = products[0]; + } else { + sums[i][j] = sums[i][j-1] + products[j-1]; + } + } + } + + int32_t profit[n_products+1][dividers+1]; + uint16_t p, d; + for (p = 0; p <= n_products; p++) { + for (d = 0; d <= dividers; d++) { + if (p == 0) { + profit[p][d] = 0; + } else if (d == 0) { + profit[p][d] = sums[0][p] - round_ct(sums[0][p]); + } else { + int32_t max = profit[p][d-1]; + uint16_t i; + for (i = 0; i < p; i++) { + int32_t try = profit[i][d-1] + sums[i][p] - round_ct(sums[i][p]); + if (try > max) + max = try; + } + profit[p][d] = max; + } + } + } + + return profit[n_products][dividers]; +} + +int main(void) { + int n_products; + int dividers; + uint32_t sum = 0; + + scanf("%d %d", &n_products, ÷rs); + + uint16_t products[n_products]; + uint16_t i; + for (i = 0; i < n_products; i++) { + scanf("%d", &(products[i])); + sum += products[i]; + if (products[i] % 5 == 0) { + n_products--; + i--; + } + } + + printf("%d\n", sum - maximum_profit(products, n_products, dividers)); +} + 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 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; - } - -} diff --git a/Practical2/tester/test.sh b/Practical2/tester/test.sh index 4a982ac..9051e97 100755 --- a/Practical2/tester/test.sh +++ b/Practical2/tester/test.sh @@ -1,14 +1,6 @@ #!/bin/bash -java="/usr/lib/jvm/java-8-openjdk-amd64/bin/java" - -# Java -#dir="$(dirname $0)/../out/production/Practical2" -#samples="../../../tester/samples" -#cmd="$java nl.camilstaps.cs.Main" - -# C -dir="$(dirname $0)/../c" -samples="../tester/samples" +dir="$(dirname $0)/.." +samples="./tester/samples" cmd="./checkout" failed=0 -- cgit v1.2.3