aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2015-12-10 23:10:50 +0000
committerCamil Staps2015-12-10 23:10:50 +0000
commit01ae4636e53c925858b89fef452023cd814c403c (patch)
tree0c9f3d0f0bf11cdd428050f42792683a1d84632e
parentStart report practical 2 (diff)
round_profit instead of round_ct
-rw-r--r--Practical2/checkout.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/Practical2/checkout.c b/Practical2/checkout.c
index 738aa61..289a50d 100644
--- a/Practical2/checkout.c
+++ b/Practical2/checkout.c
@@ -1,30 +1,28 @@
#include <stdio.h>
#include <stdint.h>
-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;
+int8_t round_profit(uint32_t value) {
+ switch (value % 5) {
+ case 0: return 0;
+ case 1: return 1;
+ case 2: return 2;
+ case 3: return -2;
+ case 4: return -1;
}
}
int32_t maximum_profit(uint16_t products[], uint16_t n_products, uint8_t dividers) {
- int32_t profit[n_products+1][dividers+1];
+ int8_t profit[n_products+1][dividers+1];
uint16_t p, d;
for (p = 0; p <= n_products; p++) {
uint32_t sums[p];
if (p == 0) {
sums[0] = 0;
} else {
+ sums[p-1] = products[p-1];
int16_t i;
- for (i = p-1; i >= 0; i--) {
- if (i == p-1)
- sums[i] = products[i];
- else
- sums[i] = products[i] + sums[i+1];
+ for (i = p-2; i >= 0; i--) {
+ sums[i] = products[i] + sums[i+1];
}
}
@@ -32,12 +30,12 @@ int32_t maximum_profit(uint16_t products[], uint16_t n_products, uint8_t divider
if (p == 0) {
profit[p][d] = 0;
} else if (d == 0) {
- profit[p][d] = sums[0] - round_ct(sums[0]);
+ profit[p][d] = round_profit(sums[0]);
} else {
- int32_t max = profit[p][d-1];
+ int8_t max = profit[p][d-1];
uint16_t i;
for (i = 0; i < p; i++) {
- int32_t try = profit[i][d-1] + sums[i] - round_ct(sums[i]);
+ int8_t try = profit[i][d-1] + round_profit(sums[i]);
if (try > max)
max = try;
}