diff --git a/ex3/main.c b/ex3/main.c index e85b866ad535c9c7b46b0f168b0d28f898c415be..e5ffd296a2579e2f74187ddce19b8f445521294f 100644 --- a/ex3/main.c +++ b/ex3/main.c @@ -1,7 +1,7 @@ /** * @file main.c - * @author Prénom Nom - * @brief Exercice 1 + * @author Florian Burgener + * @brief Exercice 3 * @version 1.0 * @date 2021-12-07 * @@ -9,20 +9,28 @@ * */ -#include <math.h> -#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> +/** + * @brief Count all the possibilities of dividing a sum of money into pieces of 2, 5 and 10. + * + * @param remaining_money + * @param available_money + * @return int32_t + */ int32_t count_posibilities(int32_t remaining_money, int32_t available_money) { - if (available_money == 1) { + if (available_money == 1) { + // No matter how much money is left, it is a possibility because you can have n * 1 pieces. return 1; } int32_t next_available_money = available_money; + // Determines which unit of money will be used for further calculations. + // 10 => 2 + // 2 => 1 if (next_available_money == 10) { next_available_money = 2; } else if (next_available_money == 2) { @@ -30,21 +38,24 @@ int32_t count_posibilities(int32_t remaining_money, int32_t available_money) { } int32_t count = 0; - int32_t i = 1; + int32_t i = 0; - while (remaining_money >= i * available_money) { + // As long as the multiplication of i by the current money unit is smaller than or equal to the remaining money, continue to loop. + while (remaining_money >= i * available_money) { + // Calls back the function by withdrawing the right amount of money. count += count_posibilities(remaining_money - available_money * i, next_available_money); i += 1; } - - count += count_posibilities(remaining_money, next_available_money); + return count; } int main() { int32_t input; scanf("%d", &input); + int32_t count = count_posibilities(input, 10); printf("\n%d\n", count); + return EXIT_SUCCESS; } \ No newline at end of file