IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

permutations/combinaisons sur des tableaux dynamiques


Sujet :

Langage Delphi

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 3
    Points : 2
    Points
    2
    Par défaut permutations/combinaisons sur des tableaux dynamiques
    Bonjour,
    je vous soumet un problème que je n'arrive pas a résoudre malgrès bien des recherches et des résultats qui ne s'avèrent pas fonctionnels...

    Le problème:

    J'ai un ensemble d'entiers de cardinal n. (tableau dynamique a une dim)
    je veux stocker dans une matrice dynamique toutes les combinaisons possibles de ces éléments. (un tableau dynamique a une dim avec pour cellule des tableaux dynamiques a une dim)
    pour info les entiers représentent des numéros de chansons dans une playlist et les combinaisons leur ordre de passage possibles. (inutile vous me direz mais ca a son utilité dans mon programme)

    en clair :

    ensemble : 4 5 7
    combis :
    4 5 7
    5 7 4
    7 5 4
    7 4 5
    4 7 5
    5 4 7

    merci si vous avez une idée
    j'ai trouvé des éléments de réponse dans les forums mais je n'arrive pas a les adapter..

  2. #2
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut ,

    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    //permet de savoir si la ranger existe deja 
     
    Function ChercheElem(aval : Tab;tb : TabSoluce): Boolean;
     var
       ii    : integer;
       found : Boolean;
     begin
       ii:= low(tb);
       While ii < high(tb) and not(found) do
       begin
          nbFound := 0;
          jj := low(tb[ii]);
          While jj < high(tb[ii]) Do
          begin
            if tb[ii][jj] = aval[jj] Then
              Inc(nbFound);
            Inc(jj);
          end;
          If nbFound = Length(aval) Then
            Found := True;
          Inc(ii);
       end;
     end;
     
    function factorielle ( n : integer ) : longint ;
    begin
      if n<2 then
        factorielle:=1
      else
        factorielle:=n*factorielle(n-1)
    end;
     
    procedure TForm1.Button9Click(Sender: TObject);
    Type
      Tab = array of integer;
      TabSoluce = array of Tab;
    var
     Tab1,tmpSoluce  : Tab;
     TbSol : TabSoluce;
    begin
      nbValue := 3;
      setlength(Tab1,nbValue);
      Tab1[0] := 4;
      Tab1[1] := 5;
      Tab1[2] := 7;
      setlength(TbSol,factorielle(nbValue));
      for i:=low(TbSol) to high(TbSol) do
        setlength(TbSol[i],nbValue)
     
    //on vient de definir les taille necessaire des different elements
    ... il ne reste plus que la repartition a faire
    je pense qu'il faut que tu passe sur une ranger tampon
    verifier si elle exite dans le tableau final
    pour ce faire utilise des boucle while
    dans lequel tu declalera les element de base

    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Merci beaucoup mais mon probleme c'est justement dans ces boucles while, en effet lorsque qu'il trouve un elément dans la rangée je lui dit de ne pas le remarquer mais du coup il passe au suivant...

    je poste mon code si tu peut voir ou est le soucis:
    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    function DANS_TABLEAU(combi:T_combi;Cardelem,element:shortint):boolean;
    var
    i:integer;
    begin
      DANS_TABLEAU:=FALSE;
      for i:=0 to Carditi-1 do begin
        if combi[i]=element then
          DANS_TABLEAU:=TRUE;
      end;
    end;
     
    function FACTORIELLE(n:shortint):shortint;
    begin
        if n = 0 then
      Result := 1
      else
      Result := n * Factorielle(n-1);
    end;
     
    procedure COMBINAISON_POSSIBLE(matcombi:T_matcombi;elements:T_combi);
    var
    i,j,k:shortint;
    begin
    for i:=1 to Cardelem do begin
      for j:=1 to Cardelem do begin
        for k:=((j-1)*FACTORIELLE(Cardelem-i)+1) to (j*FACTORIELLE(Cardelem-i)) do begin
        if not(DANS_TABLEAU(matcombi[k-1],Cardelem,elements[j-1])) then
        matcombi[k-1,i-1]:=elements[j-1];
        end;
      end;
    end;
    end;
     
    //appel
        setlength(elem,4);
        setlength(combi,24,4);
        elem[0]:=1;
        elem[1]:=2;
        elem[2]:=3;
        elem[3]:=4;
        cardelem:=4;
        COMBINAISON_POSSIBLE(combi,elem);
    EDIT: En fait cette méthode remplit les colonnes de cette facon :
    ELEMS 1 2 3

    |1 |2 |3
    |1 |3 |2

    |2 |1 |3
    |2 |3 |1

    |3 |1 |2
    |3 |2 |1
    enfin c'est ce que ca devrait faire mais j'arrive pas quand il voit qu'il y a l'element a passer a l'elemennt suivant.

  4. #4
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    voila ma solution pour ton probleme

    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
     
    Type
      Tab = array of integer;
      TabSoluce = array of Tab;
     
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Déclarations privées }
        procedure combinaisons(atab : tab;var ASoluce : TabSoluce);
        Procedure AfficheTab(ASoluce : TabSoluce);
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    function mattostr(atab : tab) : String;
    var
      z : integer;
    begin
      Result :='';
      for z:= low(atab) to high(atab) do
        Result := Result+inttoStr(atab[z]);
    end;
     
    function strtomat(ch : String;lg : integer) : Tab;
    var
      w : integer;
    begin
      setlength(result,lg);
      for w:=0 to pred(length(ch)) do
        Result[w] := strtoint(ch[w+1]);
    end;
     
    procedure EchangeCar(var ch: string; i, j: byte);
    var
      car: char;
    begin
      if i <> j then
      begin
        car   := ch[i];
        ch[i] := ch[j];
        ch[j] := car;
      end
    end;
     
    procedure Permutation(ch: string; i: byte;lg : integer;var ASoluce : TabSoluce;var indice : integer);
    var
      j: byte;
    begin
      if i = lg then
      begin
        ASoluce[indice] := strtomat(ch,lg);
        inc(indice);
      end
      else
       for j := i to lg do
       begin
         EchangeCar(ch,i,j);
         Permutation(ch,i+1,lg,ASoluce,indice);
         EchangeCar(ch, i,j);
       end;
    end;
     
    function factorielle ( n : integer ) : longint ;
    begin
     if n<2 then
       factorielle:=1
     else
       factorielle:=n*factorielle(n-1)
    end;
    procedure Setlengthmat(var ASoluce : TabSoluce;nbValue : integer);
    var
      w : integer;
    begin
      setlength(ASoluce,factorielle(nbValue));
      for w:=low(ASoluce) to high(ASoluce) do
        setlength(ASoluce[w],nbValue);
    end;
     
    procedure TForm1.combinaisons(atab : tab ;var ASoluce : TabSoluce);
    var
      ch : string;
      l  : byte;
      indice : integer;
     
    begin
      ch := '';
      memo1.Lines.Clear;
      ch := mattostr(atab);
      l := length(ch);
      indice := 0;
      Permutation(ch,1,l,ASoluce,indice);
    end;
     
    procedure TForm1.AfficheTab(ASoluce : TabSoluce);
    var
      w : integer;
    begin
      for w := low(ASoluce) to high(ASoluce) do
        Memo1.Lines.Add(mattostr(ASoluce[w]))
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      ch : Tab;
      nbValue : integer;
      w: integer;
      ATabSoluce : TabSoluce;
    begin
      nbValue := 3;
      setlength(ch,nbValue);
      ch[0] :=  4;
      ch[1] :=  5;
      ch[2] :=  7;
      Setlengthmat(ATabSoluce,nbValue);
      combinaisons(ch,ATabSoluce);
      AfficheTab(ATabSoluce);
    end;

    @+ Phil

    PS : si le code te convient met resolue
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Merci ca marche mais pas pour des entiers de plus d'un nombre. 12 15 200
    je voulais a tout prix eviter les strings je trouvais pas ca tres propre.
    Enfin merci d'avoir perdu du temps sur mon probleme.

  6. #6
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    c'etait pourtant pas tres compliquer de transformer le string en tableau

    voila le resultat

    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
     
    function mat4tostr(atab : tab) : String;
    var
      z : integer;
    begin
      Result :='';
      for z:= low(tab)+1 to high(atab) do
        Result := Result+inttoStr(atab[z]);
    end;
     
    function mat3tostr(atab : tab) : String;
    var
      z : integer;
    begin
      Result :='';
      for z:= low(tab) to high(atab) do
        Result := Result+inttoStr(atab[z]);
    end;
     
     
    procedure EchangeCar(var Lch: tab; i, j: byte);
    var
      car: integer;
    begin
      if i <> j then
      begin
        car   := Lch[i];
        Lch[i] := Lch[j];
        Lch[j] := car;
      end
    end;
     
    function mat4tomat3(atab : tab;lg : integer) : tab;
    var
     i : integer;
    begin
     setlength(Result,lg);
     For i:= 1 to high(atab) do
      Result[i-1] :=atab[i];
    end;
    procedure Permutation(ach:Tab; i: byte;lg : integer;var ASoluce : TabSoluce;var indice : integer);
    var
      j: byte;
      ch:tab;
    begin
      if i = lg then
      begin
        ASoluce[indice] := mat4tomat3(ach,lg);//strtomat(ach,lg);
        inc(indice);
      end
      else
       for j := i to lg do
       begin
         EchangeCar(ach,(i),j);
         Permutation(ach,(i)+1,lg,ASoluce,indice);
         EchangeCar(ach,(i),j);
       end;
    end;
     
    function factorielle ( n : integer ) : longint ;
    begin
     if n<2 then
       factorielle:=1
     else
       factorielle:=n*factorielle(n-1)
    end;
    procedure Setlengthmat(var ASoluce : TabSoluce;nbValue : integer);
    var
      w : integer;
    begin
      setlength(ASoluce,factorielle(nbValue-1));
      for w:=low(ASoluce) to high(ASoluce) do
        setlength(ASoluce[w],nbValue-1);
    end;
     
    procedure TForm1.combinaisons(atab : tab ;var ASoluce : TabSoluce);
    var
      l  : byte;
      indice : integer;
    begin
      memo1.Lines.Clear;
      l := length(atab);
      indice := 0;
      Permutation(atab,1,l-1,ASoluce,indice);
    end;
     
    procedure TForm1.AfficheTab(ASoluce : TabSoluce);
    var
      w : integer;
    begin
      for w := low(ASoluce) to high(ASoluce) do
        Memo1.Lines.Add(mat3tostr(ASoluce[w]))
    end;
     
    procedure TForm1.Button9Click(Sender: TObject);
    var
      ch : Tab;
      nbValue : integer;
      w: integer;
      ATabSoluce : TabSoluce;
    begin
      nbValue := 4;
      setlength(ch,nbValue);
      ch[0] :=  0;
      ch[1] :=  200;
      ch[2] :=  5;
      ch[3] :=  400;
      Setlengthmat(ATabSoluce,nbValue);
      combinaisons(ch,ATabSoluce);
      AfficheTab(ATabSoluce);
    end;

    la petite astuce est de declarer un tableau avec un indice suplementaire
    a la position zero il n'y as rien

    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 18/04/2005, 21h12
  2. Réponses: 14
    Dernier message: 13/07/2004, 13h58
  3. Article sur les tableaux dynamiques
    Par Eric Sigoillot dans le forum Langage
    Réponses: 2
    Dernier message: 16/04/2004, 22h00
  4. Réponses: 2
    Dernier message: 19/08/2003, 18h04
  5. free sur des tableaux "a moitié dynamiques"
    Par barthelv dans le forum C
    Réponses: 4
    Dernier message: 31/07/2003, 15h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo