forked from menault/2026_TD3_DEV51_Qualite_Algo
fonction 8 : O(S x k)
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
|
||||
@@ -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) */
|
||||
|
||||
Reference in New Issue
Block a user