Calcule de combinaison appartir d un tableau

Fermé
yassir - 12 sept. 2019 à 11:05
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 - 12 sept. 2019 à 21:30
Bonjour,

je veux crée cette combinaison iteration
1. A1 = A2 and B1 = B2 and C1 = C2 and D1 = D2
2. A1 = A2 and B1 = B2 and C1 = C2 and D2 = null
3. A1 = A2 and B1 = B2 and C2 = null and D1 = D2
4. A1 = A2 and B2 = null and C1 = C2 and D1 = D2
5. A2 = null and B1 = B2 and C1 = C2 and D1 = D2
6. A1 = A2 and B1 = B2 and C2 = null and D2 = null
7. A1 = A2 and B2 = null and C2 = null and D1 = D2
8. A1 = A2 and B2 = null and C1 = C2 and D2 = null
...
n A2 = nul and B2 = null and C2 = nul and D2 = null

appartir de {A ,B ,C ,D}

j ai concu un algorithme basé sur le lien suivant :
https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/


Configuration: Windows / Chrome 76.0.3809.132
A voir également:

1 réponse

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
Modifié le 12 sept. 2019 à 21:33
Bonjour,

Pas besoin de se prendre trop la tête avec un algorithme générique, c'est très scolaire et ça peut s'écrire directement avec des boucles :

for (int a = 0; a < 2; a++)
    for (int b = 0; b < 2; b++)
        for (int c = 0; c < 2; c++)
            for (int d = 0; d < 2; d++)
                System.out.printf("%s and %s and %s and %s\n",
                    a == 0 ? "A1 = A2" : "A2 = null",
                    b == 0 ? "B1 = B2" : "B2 = null",
                    c == 0 ? "C1 = C2" : "C2 = null",
                    d == 0 ? "D1 = D2" : "D2 = null");
0