diff options
| author | Abigail <abigail@abigail.be> | 2021-07-07 16:48:34 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-09 01:35:20 +0200 |
| commit | 3fde608ec96122d4f73f1579d2f6256dcb796c28 (patch) | |
| tree | 4734418b2b1c19dafe305bee23a603075ffea151 /challenge-075 | |
| parent | b2405975ce37d7db5e31deef0df94effa723e4cc (diff) | |
| download | perlweeklychallenge-club-3fde608ec96122d4f73f1579d2f6256dcb796c28.tar.gz perlweeklychallenge-club-3fde608ec96122d4f73f1579d2f6256dcb796c28.tar.bz2 perlweeklychallenge-club-3fde608ec96122d4f73f1579d2f6256dcb796c28.zip | |
C solutions for week 075
Diffstat (limited to 'challenge-075')
| -rw-r--r-- | challenge-075/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-075/abigail/c/ch-1.c | 65 | ||||
| -rw-r--r-- | challenge-075/abigail/c/ch-2.c | 84 |
3 files changed, 151 insertions, 0 deletions
diff --git a/challenge-075/abigail/README.md b/challenge-075/abigail/README.md index cb325bba30..3e09a6853a 100644 --- a/challenge-075/abigail/README.md +++ b/challenge-075/abigail/README.md @@ -27,6 +27,7 @@ f) (2, 4) ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Perl](perl/ch-1.pl) @@ -76,4 +77,5 @@ is formed by columns `(5, 7 and 5)`. ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [C](c/ch-2.c) * [Perl](perl/ch-2.pl) diff --git a/challenge-075/abigail/c/ch-1.c b/challenge-075/abigail/c/ch-1.c new file mode 100644 index 0000000000..ccf201b68a --- /dev/null +++ b/challenge-075/abigail/c/ch-1.c @@ -0,0 +1,65 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int possibilities (int target, int * coins, int from, int to) { + if (target == 0) { + return 1; + } + if (target < 0 || from > to) { + return 0; + } + + int sum = 0; + for (int i = 0; i * coins [from] <= target; i ++) { + sum += possibilities (target - i * coins [from], coins, from + 1, to); + } + + return sum; +} + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + int coin; + int n; + int offset = 0; + int c = 0; + while (sscanf (line + offset, "%d%n", &coin, &n) == 1) { + offset += n; + c ++; + } + /* + * Now c will be the amount of integers on the line. + * We need this to determine the size of the to be + * allocated array + */ + int * coins; + if ((coins = (int *) malloc (c * sizeof (int))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + offset = 0; + c = 0; + while (sscanf (line + offset, "%d%n", &coin, &n) == 1) { + offset += n; + coins [c ++] = coin; /* If c == 0, it's our target */ + } + + printf ("%d\n", possibilities (coins [0], coins, 1, c - 1)); + } + free (line); + + return (0); +} diff --git a/challenge-075/abigail/c/ch-2.c b/challenge-075/abigail/c/ch-2.c new file mode 100644 index 0000000000..5fda685e14 --- /dev/null +++ b/challenge-075/abigail/c/ch-2.c @@ -0,0 +1,84 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + int height; + int n; + int offset = 0; + int c = 0; + while (sscanf (line + offset, "%d%n", &height, &n) == 1) { + offset += n; + c ++; + } + /* + * Now c will be the amount of integers on the line. + * We need this to determine the size of the to be + * allocated array + */ + int * heights; + if ((heights = (int *) malloc (c * sizeof (int))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + offset = 0; + c = 0; + int max_height = 0; + while (sscanf (line + offset, "%d%n", &height, &n) == 1) { + offset += n; + heights [c ++] = height; + if (max_height < height) { + max_height = height; + } + } + + /* + * For each height, find the longest sequence of consecutive + * columns with at least that height, and calculate the + * rectangle with that height. Remember the largest. + */ + + int max_area = 0; + for (int h = 1; h <= max_height; h ++) { + int cur = 0; + int max = 0; + for (int i = 0; i < c; i ++) { + if (heights [i] >= h) { + cur ++; + } + else { + if (max < cur) { + max = cur; + } + cur = 0; + } + } + if (max < cur) { + max = cur; + } + + int area = max * h; + if (max_area < area) { + max_area = area; + } + } + + printf ("%d\n", max_area); + } + free (line); + + return (0); +} |
