Bonjour,

Combien coût le code suivant en fonction de nombre d'opérations (complexité) sachant d'afficher toutes les combinaisons de N attributs ?



Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
 
#define N 5
void afficher(int etat[], char *t[])
{
  int i;
  for (i = 0; i < N; i++)
    if (etat[i])
      printf("%s ", t[i]);
  puts("");
}
 
void partie(int h, int etat[], char *t[])
{
  enum { ABSENT, PRESENT };
  if (h < 0)
    afficher(etat, t);
  else
    {
      etat[h] = ABSENT;
      partie(h - 1, etat, t);
      etat[h] = PRESENT;
      partie(h - 1, etat, t);
    }
}
 
int main(void)
{
  char *t[N] = { "nom", "prenom", "age", "adresse", "emploi" };
  int etat[N];
  partie(N - 1, etat, t);
  return 0;
}