From d40041b1cf693956cd47ab6cdfef6274a58b08a1 Mon Sep 17 00:00:00 2001 From: Florian Burgener <florian.burgener@hesge.ch> Date: Tue, 7 Dec 2021 14:39:13 +0100 Subject: [PATCH] Validation exercice 3 --- ex3/main.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/ex3/main.c b/ex3/main.c index e85b866..e5ffd29 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 -- GitLab