Skip to content
Snippets Groups Projects
Commit d40041b1 authored by Florian Burgener's avatar Florian Burgener
Browse files

Validation exercice 3

parent de6fb34d
Branches
No related tags found
No related merge requests found
/**
* @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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment