From 8e63bf1c181e1391d1da4b37b4ec3be64a3ff17b Mon Sep 17 00:00:00 2001 From: camille Date: Wed, 21 Oct 2026 12:14:04 +0200 Subject: [PATCH] optimisation de F2 en utilisant l'id comme index d'un tableau de booleens --- banque.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 11 deletions(-) diff --git a/banque.c b/banque.c index aff9e89..1145aa9 100644 --- a/banque.c +++ b/banque.c @@ -118,24 +118,95 @@ void trier_par_montant(Transaction *t, int n) { } } + + + + /* F2 : nombre de clients ayant au moins une transaction */ -int nb_clients_distincts(const Transaction *t, int n) + + + + +int nb_clients_distincts(const Transaction *t, int n) // on utilise l'id come index { - int nb = 0; - for (int i = 0; i < n; i++) { - int deja_vu = 0; - for (int j = 0; j < i; j++) { - if (t[j].client == t[i].client) { - deja_vu = 1; - break; - } + + + + if (n <= 0) return 0; + + + + + // on trouve l'id/index max pr la taille du tableau + + int max_id = t[0].client; + + + for (int i = 1; i < n; i++) + { + + if (t[i].client > max_id) + { + + max_id = t[i].client; + } - if (!deja_vu) - nb++; + } + + + + + // creation du tableau + + bool *presence = (bool *)calloc(max_id + 1, sizeof(bool)); + + + if (presence == NULL) // si le tableau est vide (ne devrait pas arriver sauf si j'ai mal fais les choses) + { + + return -1; // on sort direct de la fonction + + } + + + + + // on compte les clients + + int nb = 0; + + + for (int i = 0; i < n; i++) + { + + int id_client = t[i].client; + + if (!presence[id_client]) // si client pas vu + { + + presence[id_client] = true; // marquer comme vu + nb++; + + } + } + + + free(presence); // j'ai du mal avc la memoire dcp honnetement c gemini qui m'a dit de faire ca (cleo) + return nb; + } + + + + + + + + + /* F3 : solde total de chaque client */ void total_par_client(const Transaction *t, int n, long *totaux) {