1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00

feat(solutions): add cakes-swerc-2020-2021/c/function

This commit is contained in:
Théo LUDWIG 2023-08-21 23:52:03 +02:00
parent 25c07b1858
commit 2a98eab556
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
7 changed files with 146 additions and 3 deletions

View File

@ -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

View File

@ -0,0 +1,3 @@
# cakes-swerc-2020-2021/c/function
Created by [@theoludwig](https://github.com/theoludwig) on 21 August 2023.

View File

@ -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';
}

View File

@ -0,0 +1,16 @@
#ifndef __CHARACTER__
#define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/**
* @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

View File

@ -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;
}

View File

@ -0,0 +1,16 @@
#ifndef __INPUT__
#define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -0,0 +1,87 @@
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}