Combinaison

RONIBO

XLDnaute Impliqué
Bonjour,

J'aimerais obtenir la combinaison des mots,

J'aimerais le personnalisé, possibilité d'avoir les résultats minimum avec deux mots et maximum avec huit mots.

Ex : (On considère les lettres comme mot)

A
B
C
D
E
F
G
H

Combinaison :
BCDEFGH
ACDEFGH
ABDEFGH

Etc..

Je vous mets un fichier exemple.

Merci d'avance.
 

Pièces jointes

  • Classeur2.xlsm
    8.7 KB · Affichages: 63
  • Classeur2.xlsm
    8.7 KB · Affichages: 62
  • Classeur2.xlsm
    8.7 KB · Affichages: 72

Modeste geedee

XLDnaute Barbatruc
Re : Combinaison

Bonsour®
Re,

c'était plus un essai sur les fonctions récursives , limité à un max de 12 objets (au delà plantage dans la fonction factoriel), l'affichage sur feuille ne devrait pas poser de soucis.

A+
;) c'est de la diversité que vient la ludicité...:cool:
 

Pièces jointes

  • Capture.JPG
    Capture.JPG
    97.8 KB · Affichages: 67
  • Capture.JPG
    Capture.JPG
    97.8 KB · Affichages: 62
  • Capture.JPG
    Capture.JPG
    97.8 KB · Affichages: 61

job75

XLDnaute Barbatruc
Re : Combinaison

Bonjour à tous,

Une macro assez bourrin qui peut traiter jusqu'à 15 éléments :

Code:
Sub Combinaisons(texte, N)
Dim s, P%, Nc&, t$(), temp$(), x1%, x2%, x3%, x4%, x5%, x6%
Dim x7%, x8%, x9%, x10%, x11%, x12%, x13%, x14%, x15%, m&
Range("C2:C" & Rows.Count).ClearContents 'RAZ
N = Int(Val(CStr(N)))
If CStr(texte) = "" Or N < 1 Then Exit Sub
s = Split(CStr(texte), "-")
P = UBound(s) + 1
If P > 15 Then MsgBox "Maximum 15 éléments !", 48: Exit Sub
If N > P Then Exit Sub
Nc = Application.Combin(P, N) 'nombre de combinaisons
ReDim t(1 To Nc, 1 To 1)
ReDim temp(1 To N)
For x1 = 0 To P - N
  temp(1) = s(x1)
  If N < 2 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 1
For x2 = x1 + 1 To P - N + 1
  temp(2) = s(x2)
  If N < 3 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 2
For x3 = x2 + 1 To P - N + 2
  temp(3) = s(x3)
  If N < 4 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 3
For x4 = x3 + 1 To P - N + 3
  temp(4) = s(x4)
  If N < 5 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 4
For x5 = x4 + 1 To P - N + 4
  temp(5) = s(x5)
  If N < 6 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 5
For x6 = x5 + 1 To P - N + 5
  temp(6) = s(x6)
  If N < 7 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 6
For x7 = x6 + 1 To P - N + 6
  temp(7) = s(x7)
  If N < 8 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 7
For x8 = x7 + 1 To P - N + 7
  temp(8) = s(x8)
  If N < 9 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 8
For x9 = x8 + 1 To P - N + 8
  temp(9) = s(x9)
  If N < 10 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 9
For x10 = x9 + 1 To P - N + 9
  temp(10) = s(x10)
  If N < 11 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 10
For x11 = x10 + 1 To P - N + 10
  temp(11) = s(x11)
  If N < 12 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 11
For x12 = x11 + 1 To P - N + 11
  temp(12) = s(x12)
  If N < 13 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 12
For x13 = x12 + 1 To P - N + 12
  temp(13) = s(x13)
  If N < 14 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 13
For x14 = x13 + 1 To P - N + 13
  temp(14) = s(x14)
  If N < 15 Then m = m + 1: t(m, 1) = Join(temp, "-"): GoTo 14
For x15 = x14 + 1 To P - N + 14
  temp(15) = s(x15)
  m = m + 1: t(m, 1) = Join(temp, "-")
Next
14 Next
13 Next
12 Next
11 Next
10 Next
9 Next
8 Next
7 Next
6 Next
5 Next
4 Next
3 Next
2 Next
1 Next
[C2].Resize(Nc) = t
End Sub
Il n'est guère difficile de prolonger la macro pour traiter plus de 15 éléments.

Fichier joint avec les macros dans le code de la feuille.

A+
 

Pièces jointes

  • Combinaisons(1).xls
    373.5 KB · Affichages: 54
  • Combinaisons(1).xls
    373.5 KB · Affichages: 53
  • Combinaisons(1).xls
    373.5 KB · Affichages: 66
Dernière édition:

Modeste geedee

XLDnaute Barbatruc
Re : Combinaison

Bonsour®
je rebondissais simplement au fait que Paf avait supprimé sa proposition.

une proposition aussi farfelue soit-elle, hormis le cas d'être complétement hors de propos, est toujours susceptible d'amélioration ou évolution et peut inspirer aux esprits constructifs ou ludiques des adaptations et détournement à d'autres problématiques...
:cool:
 

KenDev

XLDnaute Impliqué
Re : Combinaison

Bonjour à tous,

Un code qui affiche assez vite les 109600 (= Sigma(Permut(8,i), i= 1 -> 8 )) mots de l'exemple proposé sur une nouvelle feuille en colonne 1.

Cordialement

KD

VB:
Option Explicit
Sub Rn()
    Dim a$, b&, i&, c&(), d$(), j&, k&, r&, e$()
    a = "ABCDEFGH"
    Application.Calculation = xlCalculationManual: Application.ScreenUpdating = False
    b = Len(a): ReDim d(1 To b): ReDim e(0)
    For i = 1 To b: d(i) = Right(Left(a, i), 1): Next i
    For i = 1 To b
        c = PerN(b, i): ReDim Preserve e(r + UBound(c))
        For j = 1 To UBound(c)
            e(r + j) = d(c(j, 1))
            For k = 2 To UBound(c, 2): e(r + j) = e(r + j) & d(c(j, k)): Next k
        Next j
        r = UBound(e)
    Next i
    Sheets.Add
    For i = 1 To UBound(e): Cells(i, 1) = e(i): Next i
    Application.Calculation = xlCalculationAutomatic: Application.ScreenUpdating = True
End Sub
Function PerN(ByVal a&, ByVal b&) As Long()             'permutations
    Dim Tc&(), Tp&(), Uc&, Up&, c&, t&, r&, i&, j&, k&, ii&
    Tc = CmbN(a, b): Uc = UBound(Tc): Up = FacL(a, b): ReDim Tp(1 To Up, 1 To b)
    For i = 1 To Uc: For j = 1 To b: Tp(i, j) = Tc(i, j): Next j, i
    Erase Tc: c = Uc
    For ii = 1 To b - 1
        r = c
        For i = 1 To r
            ReDim Tc(ii To b)
            For j = ii To b: Tc(j) = Tp(i, j): Next j
            For j = 1 To b - ii
                c = c + 1: t = Tc(ii)
                For k = 1 To ii - 1: Tp(c, k) = Tp(i, k): Next k
                For k = ii To b - 1: Tp(c, k) = Tc(k + 1): Next k
                Tp(c, b) = t
                If j < b - ii Then
                    For k = ii To b: Tc(k) = Tp(c, k): Next k
                End If
            Next j
        Next i
    Next ii
    PerN = Tp
End Function
Function CmbN(ByVal a&, ByVal b&) As Long()                         'combinaisons(a,b)
    Dim n&, Tb&(), c&, i&, j&
    n = CmbNb(a, b): ReDim Tb(1 To n, 1 To b): c = a - b
    For i = 1 To b: Tb(1, i) = i: Next i
    For i = 2 To n
        If b = 1 Then Tb(i, 1) = Tb(i - 1, 1) + 1 Else Tb(i, 1) = Tb(i - 1, 1) - (Tb(i - 1, 2) = c + 2)
        For j = 2 To b - 1
            If Tb(i - 1, j + 1) = c + j + 1 Then
                If Tb(i - 1, j) = c + j Then Tb(i, j) = Tb(i, j - 1) + 1 Else Tb(i, j) = Tb(i - 1, j) + 1
            Else
                Tb(i, j) = Tb(i - 1, j)
            End If
        Next j
        If Tb(i - 1, b) = a Then Tb(i, b) = Tb(i, b - 1) + 1 Else Tb(i, b) = Tb(i - 1, b) + 1
    Next i
    CmbN = Tb
End Function
Function CmbNb(ByVal a&, ByVal b&) As Long                          'nb combinaisons(a,b)
    Dim c&
    c = a - b
    If c = 0 Then
        CmbNb = 1
    Else
        If b < c Then c = b
        CmbNb = FacL(a, c) / FacL(c)
    End If
End Function
Function FacL(ByVal a&, Optional b) As Long                         'factorielle (option nb itérations)
    Dim i&, c&
    If Not IsMissing(b) Then c = CLng(b) Else c = a
    If a = 0 Or c = 0 Then FacL = 1: Exit Function
    FacL = a
    For i = 2 To c: FacL = FacL * (a - i + 1): Next i
End Function
 

Discussions similaires

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 756
Messages
2 091 734
Membres
105 060
dernier inscrit
DEDJAN Gaston