diff --git a/challenges/cakes-swerc-2020-2021/README.md b/challenges/cakes-swerc-2020-2021/README.md index 69df1da..3e4b110 100644 --- a/challenges/cakes-swerc-2020-2021/README.md +++ b/challenges/cakes-swerc-2020-2021/README.md @@ -25,10 +25,10 @@ this ingredient you have in your kitchen. The output should contain a single integer: the maximum number of cakes you can make using the available ingredients. -### Limits +### Constraints -- 1 <= `N` <= 10 -- All ingredient quantities will be integers between 1 and 10 000 +- $$1 <= N <= 10$$ +- All ingredient quantities will be integers between 1 and 10 000. ## Source diff --git a/challenges/cakes-swerc-2020-2021/solutions/c/function/README.md b/challenges/cakes-swerc-2020-2021/solutions/c/function/README.md new file mode 100644 index 0000000..a14d7a7 --- /dev/null +++ b/challenges/cakes-swerc-2020-2021/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# cakes-swerc-2020-2021/c/function + +Created by [@theoludwig](https://github.com/theoludwig) on 21 August 2023. diff --git a/challenges/cakes-swerc-2020-2021/solutions/c/function/character.c b/challenges/cakes-swerc-2020-2021/solutions/c/function/character.c new file mode 100644 index 0000000..3e8c0ff --- /dev/null +++ b/challenges/cakes-swerc-2020-2021/solutions/c/function/character.c @@ -0,0 +1,7 @@ +#include "character.h" + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} diff --git a/challenges/cakes-swerc-2020-2021/solutions/c/function/character.h b/challenges/cakes-swerc-2020-2021/solutions/c/function/character.h new file mode 100644 index 0000000..e3a2037 --- /dev/null +++ b/challenges/cakes-swerc-2020-2021/solutions/c/function/character.h @@ -0,0 +1,16 @@ +#ifndef __CHARACTER__ +#define __CHARACTER__ + +#include +#include + +/** + * @brief Append a character to a string, assuming string points to an array + * with enough space. + * + * @param string + * @param character + */ +void character_append(char* string, char character); + +#endif diff --git a/challenges/cakes-swerc-2020-2021/solutions/c/function/input.c b/challenges/cakes-swerc-2020-2021/solutions/c/function/input.c new file mode 100644 index 0000000..0e4515c --- /dev/null +++ b/challenges/cakes-swerc-2020-2021/solutions/c/function/input.c @@ -0,0 +1,14 @@ +#include "input.h" + +char* input() { + char character; + size_t length = 1; + char* string = malloc(length * sizeof(char)); + *string = '\0'; + while ((character = getchar()) != '\n' && character != EOF) { + length++; + string = realloc(string, length * sizeof(char)); + character_append(string, character); + } + return string; +} diff --git a/challenges/cakes-swerc-2020-2021/solutions/c/function/input.h b/challenges/cakes-swerc-2020-2021/solutions/c/function/input.h new file mode 100644 index 0000000..180ae36 --- /dev/null +++ b/challenges/cakes-swerc-2020-2021/solutions/c/function/input.h @@ -0,0 +1,16 @@ +#ifndef __INPUT__ +#define __INPUT__ + +#include +#include + +#include "character.h" + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/cakes-swerc-2020-2021/solutions/c/function/solution.c b/challenges/cakes-swerc-2020-2021/solutions/c/function/solution.c new file mode 100644 index 0000000..5166316 --- /dev/null +++ b/challenges/cakes-swerc-2020-2021/solutions/c/function/solution.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +#include "input.h" + +size_t string_get_length(const char* string) { + size_t length = 0; + while (string[length] != '\0') { + length++; + } + return length; +} + +char* string_copy(const char* string) { + size_t source_length = string_get_length(string); + char* copy = malloc(sizeof(char) * (source_length + 1)); + if (copy == NULL) { + perror("Error (string_copy)"); + exit(EXIT_FAILURE); + } + size_t index; + for (index = 0; index < source_length; index++) { + copy[index] = string[index]; + } + copy[index] = '\0'; + return copy; +} + +char** string_split(const char* string, char separator, size_t* result_size) { + size_t string_length = string_get_length(string); + size_t index_string = 0; + size_t index_current = 0; + size_t index_result = 0; + char* current = malloc(sizeof(char) * (string_length + 1)); + char** result = NULL; + if (current == NULL) { + perror("Error (string_split)"); + exit(EXIT_FAILURE); + } + while (index_string < string_length) { + if (string[index_string] == separator) { + current[index_current] = '\0'; + result = realloc(result, sizeof(char*) * (index_result + 1)); + if (result == NULL) { + perror("Error (string_split)"); + exit(EXIT_FAILURE); + } + result[index_result] = string_copy(current); + index_result++; + index_current = 0; + } else { + current[index_current] = string[index_string]; + index_current++; + } + index_string++; + } + current[index_current] = '\0'; + result = realloc(result, sizeof(char*) * (index_result + 1)); + if (result == NULL) { + perror("Error (string_split)"); + exit(EXIT_FAILURE); + } + result[index_result] = string_copy(current); + free(current); + *result_size = index_result + 1; + return result; +} + +int main() { + size_t maximum_number_of_cake_possible = 0; + size_t number_of_ingredients = (size_t)atoi(input()); + for (size_t ingredient_index = 0; ingredient_index < number_of_ingredients; ingredient_index++) { + char* string = input(); + size_t ingredient_size = 0; + char** ingredient = string_split(string, ' ', &ingredient_size); + int quantity_per_cake = atoi(ingredient[0]); + int quantity_available = atoi(ingredient[1]); + size_t cake_possible = quantity_available / quantity_per_cake; + if (ingredient_index == 0 || maximum_number_of_cake_possible > cake_possible) { + maximum_number_of_cake_possible = cake_possible; + } + } + printf("%zu\n", maximum_number_of_cake_possible); + return EXIT_SUCCESS; +}