Ensemble des parties

Résolu
cs_habbou Messages postés 17 Date d'inscription dimanche 3 septembre 2006 Statut Membre Dernière intervention 14 novembre 2006 - 1 oct. 2006 à 16:29
cs_habbou Messages postés 17 Date d'inscription dimanche 3 septembre 2006 Statut Membre Dernière intervention 14 novembre 2006 - 2 oct. 2006 à 16:46
Bonjour à  tous ;
je cherche un algorithme qui me permet de renvoyer l'ensemble de partie .

par exemple :

input:{a,b,c}
output:ensemble des parties={ vide, a,b,c,ab,ac,bc,abc}

2 réponses

cs_habbou Messages postés 17 Date d'inscription dimanche 3 septembre 2006 Statut Membre Dernière intervention 14 novembre 2006
2 oct. 2006 à 16:46
Bonjour  vychnou;
Merci a vous .J'ai trouvé la solution
Voici la solution. et encord merci
<ol class="csCode"><li>
class
combinaisons{
</li><li> <samp>//renvoi toutes les combinaisons de t booléens</samp></li><li> 
static
boolean
[][]Combinaisons(
int
t){<samp>//t=taille de la chaine</samp></li><li>     
boolean
[][]comb=
new
boolean
[(
int
)
Math
.pow(2,t)][t];
</li><li>     
for
(
int
i=0;i<comb.
length
;i++){
</li><li>      
int
a=i;
</li><li>      
for
(
int
j=comb[0].
length
-1;j>=0;j--){
</li><li>       
if
(a%2==1){
</li><li>        comb[i][j]=
true
;
</li><li>        a=(
int
)(a/2);
</li><li>       }
</li><li>       
else
{
</li><li>        a/=2;
</li><li>       }
</li><li>      }
</li><li>     }
</li><li>     
return
comb;
</li><li>    }
</li><li>    
</li><li>    <samp>//renvoi toutes les combinaisons d'une chaine de caractère</samp></li><li>    
static
char
[][] Combinaisons(
String
mot){
</li><li>     
int
longueur=mot.
length
();
</li><li>  
int
nbr=(
int
)
Math
.pow(2,longueur);<samp>//nbr=nombre de combinaisons</samp></li><li>     
char
[][] comb=
new
char
[nbr][longueur];
</li><li>     
int
k=0;
</li><li>     
for
(
int
i=0;i<nbr;i++){
</li><li>      k=i;
</li><li>      
for
(
int
j=longueur-1;j>=0;j--){
</li><li>       
if
(k%2==0){
</li><li>        k/=2;
</li><li>       }
</li><li>       
else
{
</li><li>        comb[i][j]=mot.charAt(j);
</li><li>        k/=2;
</li><li>       }
</li><li>      }
</li><li>     }
</li><li>     
return
comb;
</li><li>    }
</li><li>    
static
void
afficher(
char
[][] a){
</li><li>     
for
(
int
i=0;ilength</code>;i++){
</li><li>      
for
(
int
j=0;jlength</code>;j++){
</li><li>       
System
.out.print(a[i][j]);
</li><li>      }
</li><li>      
System
.out.println();
</li><li>     }
</li><li>    }
</li><li>    
static
void
afficher(
boolean
[][] a){
</li><li>     
for
(
int
i=0;ilength</code>;i++){
</li><li>      
for
(
int
j=0;jlength</code>;j++){
</li><li>       
System
.out.print(a[i][j]);
</li><li>      }
</li><li>      
System
.out.println();
</li><li>     }
</li><li>    }
</li><li>    
public
static
void
main(
String
[]adrien){
</li><li>     afficher(Combinaisons(5));
</li><li>     afficher(Combinaisons(<var>"abcd"</var>));
</li><li>    }
</li><li>} </li></ol>
3
cs_vychnou Messages postés 124 Date d'inscription dimanche 5 octobre 2003 Statut Membre Dernière intervention 11 mai 2009 18
2 oct. 2006 à 10:34
Euh j'avais fait ça en python, d'une manière "relativement" simple (je peux le poster si tu veux), sinon en java j'ai ce code à te proposer (il y a sans doute de meilleurs algorithmes):

/**
* Résoudre et afficher les résultats du pb pour un certain 'nb'
* @param int nb : nombre de couple
*/
private static void resoudrePourNb(int nb) {
// Constituer les nb lettres
String [] nLettres = null;
nLettres = new String[nb];
for (int i = 0; i < nb; i++) {
nLettres [i] = i+"";
}

// Trouver les nb! combi
String[][] combisPossibles = trouveCombis(nLettres);

// Les compter et compter les OK
float cpt = combisPossibles.length;
float cptOk = 0;
for (int i = 0; i < cpt; i++) {
int j = 0;
for (j = 0; j < combisPossibles[i].length
&& !combisPossibles [i][j].equals(j+""); j++) {
}
if (j < combisPossibles[i].length && combisPossibles [i][j].equals(j+"")) {
cptOk++;
}
}

// Afficher le résultat pour nb
float pct = cptOk/cpt;
System.out.println("Pour '" + nb + "', nb de combis OK / nb total " + cptOk + " / " + cpt + " " + pct + " %");
}

/**/
private static String[][] trouveCombis(String[] lettres) {
String[][] ret = null;
int n = lettres.length;
String[] tmp = new String[n];
int cpt = 0;
if (n >= 1) {
ret = new String[fact(n)][n];
if (n == 2) {
ret[0] = lettres;
tmp = new String[] {lettres[1], lettres[0]};
ret[1] = tmp;
} else if (n < 2) {
ret[0] = lettres;
} else {
for (int i = 0; i < lettres.length; i++) {
String[] lettreSansI = enleveI(lettres, i);
String[][] res = trouveCombis(lettreSansI);
for (int j = 0; j < res.length; j++) {
for (int k = 0; k < res[j].length; k++) {
tmp[k] = res[j][k];
}
tmp[n - 1] = lettres[i];
ret[cpt++] = tmp;
tmp = new String[n];
}
}
}
}

return ret;
}

/**
* Enlève la ième chaîne d'un tableau de chaîne
* @param String[] tabStr
* @param int i
* @return String[] ret
*/
private static String[] enleveI(String[] tabStr, int i) {
String[] ret = new String[tabStr.length - 1];
for (int j = 0; j < tabStr.length; j++) {
if (j < i) {
ret[j] = tabStr[j];
} else if (j > i) {
ret[j-1] = tabStr[j];
}
}
return ret;
}

/**
* Factorielle
* @param int n
* @return int ret
*/
private static int fact(int n) {
int ret = 1;
if (n > 1) {
for (int i = 1; i <= n; i++) {
ret = ret * i;
}
}
return ret;
}

}
0
Rejoignez-nous