optimisation de F2 en utilisant l'id comme index d'un tableau de booleens

This commit is contained in:
camille
2026-10-21 12:14:04 +02:00
parent 8cce27c295
commit 8e63bf1c18
+82 -11
View File
@@ -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)
{