fonction 8 : O(S x k)

This commit is contained in:
abraham
2026-10-21 11:44:36 +02:00
parent dc5f6dbbd9
commit b40a1c2081
3 changed files with 23 additions and 4 deletions
+1 -1
View File
@@ -98,7 +98,7 @@ La complexité de la fonction est de <span style="color:blue">[O(n)]</span> pour
- Optimisation de la fonction 7 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 8 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 8 nom du commit : "fonction 8 : O(S x k)". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 9 nom du commit : "". Emprunte : ff68d260a7a58441
BIN
View File
Binary file not shown.
+22 -3
View File
@@ -171,12 +171,31 @@ static const int PIECES[] = {1, 2, 5, 10, 20, 50, 100, 200};
long nb_facons_rendre(int somme, int nb_pieces)
{
if (somme < 0 || nb_pieces < 0)
return 0;
if (somme == 0)
return 1;
if (somme < 0 || nb_pieces == 0)
if (nb_pieces == 0)
return 0;
return nb_facons_rendre(somme - PIECES[nb_pieces - 1], nb_pieces) +
nb_facons_rendre(somme, nb_pieces - 1);
if (nb_pieces > NB_PIECES)
nb_pieces = NB_PIECES;
long *combinaisons = malloc((somme + 1) * sizeof(long));
if (combinaisons == NULL)
return 0;
for (int montant = 0; montant <= somme; montant++)
combinaisons[montant] = 0;
combinaisons[0] = 1;
for (int i = 0; i < nb_pieces; i++) {
for (int montant = PIECES[i]; montant <= somme; montant++)
combinaisons[montant] += combinaisons[montant - PIECES[i]];
}
long resultat = combinaisons[somme];
free(combinaisons);
return resultat;
}
/* F9 : catégorie d'une transaction (pour les statistiques) */