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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
package complexite;
import java.util.*;
public class complexite {
//__________________________________________________________________
public static void main(String[] args ) {
Scanner keby = new Scanner(System.in);
System.out.println("Veuillez saisir le nombre détudiants :");
// Liste des étudiants
int n = keby.nextInt();
String[][] tabEtud = new String[n][4]; // Une structure ou class aurait été préférable
System.out.println("Veuillez saisir le nombre pair détudiant incompatible :");
// Liste de paires étudiants incompatible dans le même dortoir exemple: 2 pr 4 etudiants ou 5 pr 10 etud
int n1 = keby.nextInt();
String [][] tabEtudInc = new String[n1][2];
System.out.println("Veuillez saisir le nombre détudiants à sélectionner par pair càd toatl etudiant ou 2 * nbre de chambre :");
// 2 étudiants par chambre soit 2*nombres de chambres
int m = keby.nextInt();
String [][] tabEtudSelect = new String[m][2];
String clobalId = "001";
String choix;
do {
System.out.println("------Menu principal ---------");
System.out.println(" 1. Liste détudiants ");
System.out.println(" 2.Liste détudiants incompatibles ");
System.out.println(" 3. Sélection détudiants ");
System.out.println(" 4.Quitter ");
System.out.println(" Taper votre choix : ");
choix = keby.nextLine();
switch(choix) {
case "1": saisieEtudiant (tabEtud, keby);
break;
case "2": saisieIncompatibilite (tabEtudInc, keby);
break;
case "3": Selection(tabEtudSelect, tabEtud , tabEtudInc , keby, clobalId);
break;
default : System.out.println("Veuillez respecter le menu!");
}
} while(choix != "4");
}
//__________________________________________________________________
public static void saisieEtudiant(String [][] tabEtud, Scanner keby) {
int n = tabEtud.length;
int i = 0;
do { // C'est un do while qui mime exactement un for
System.out.println("Veuillez saisir lId de létudiants :");
tabEtud[i][0] = keby.nextLine();
/* System.out.println("Veuillez saisir le nom de létudiants :");
tabEtud[i][1] = keby.nextLine();
System.out.println("Veuillez saisir le prénom de létudiants :");
tabEtud[i][2] = keby.nextLine();
System.out.println("Veuillez saisir ladresse de létudiants :");
tabEtud[i][3] = keby.nextLine(); */
i = i + 1;
} while (i < n);
}
//______________________________________________________________________________________________
public static void saisieIncompatibilite(String [][] tabEtudInc, Scanner keby) {
int n = tabEtudInc.length;
int i = 0;
do {
System.out.println("Veuillez saisir lId de létudiant1 incompatible:");
tabEtudInc[i][0] = keby.nextLine();
System.out.println("Veuillez saisir lId de létudiant2 incompatible:");
tabEtudInc[i][1] = keby.nextLine();
i = i + 1;
} while(i < n);
}
// Sélection par tirage au sort ________________________________________________________________
public static void Selection(String [][] tabEtudSelect, String [][]tabEtud,
String [][]tabEtudInc, Scanner keby,String clobalId) {
int n = tabEtud.length;
int Indice=0; // indice de id incompatible necessaire pour le test de compatibilité
int EI=tabEtudInc.length; // nombre etudiants incompatibles
String id1;
String id2;
String idRe;
int inRe;
int n1;
//int EC=n*n-1/2-EI; // nombre etudiants compatible
Boolean Ajouter = false; // Si Ajouter = vrai sortir du while
String[][] tabTirage = new String[1][2]; // 2x tirés, rangés et remplacés au tirage suivant
// Mets la liste étudiant dans ArrayList pour faciliter le traitement
ArrayList<String> tabEtudCopy = new ArrayList< String > ();
// Récupère les selections et à la sortie recopierra dans tabEtudSelect
ArrayList<String> tabEtudSelectCopy = new ArrayList< String > ();
int c = tabEtudSelect.length/2; // Nombre de chambres, doit être = m / 2 et m = tabEtudSelect.length
for(int i = 0; i < n; i++) {
tabEtudCopy.add(tabEtud[i][0]); // Transfert de la liste étudiant dans la copie
}
/*Saisie des étudiants dans des chambres; i=0 une chmbre donc deux étudiants; i=1 une seconde
chambre donc deux "tudiants encore ainsi de suite*/
if(EI==n*n-1/2) {
// si tout le monde est incompatible avec tout le monde alors on n'entre pas dans la boucle;
// en plus on a deux choix
//1-afficher le message d'information
//2-faire une seule selection et sortir
System.out.println("Selection impossible, tout le monde est incompatible avec tout le monde!");
/* OU
Random random = new Random(); // Tirage au sort ou nombre aleatoire
n1 = random.nextInt(n); // Choix dans l'intervale 0 et n
id1 = tabEtudCopy.get(n1);
tabEtudSelectCopy.add(id1); // Si id1 retenu, l'etudiant sera selectionné
*/
} else {
loop:
for(int i = 0; i < c; i++) {
Ajouter = false; // Chaque ajout egal vrai pour sortir du do while
do {
Random random = new Random(); // Tirage au sort ou nombre aleatoire
int nb;
n1 = tabEtudCopy.size(); // Determine le nombre elements restants dans la copie etudiantes; chaque selection est supprimmé dedans
if(n1 == 1){
id1 = tabEtudCopy.get(n1); // il est le seul à être logé dans une chambre
tabEtudSelectCopy.add(id1); // Il est rangé dans le tirage de façon definitive
tabEtudCopy.remove(n1); // Oté du tableau
break loop; //fin de selection
} else {
// la selection continu meme si le nombre de chambre > aux nombres compatibilites, car la selection s'arrete quand n1=0
nb = random.nextInt(n1); // Choix dans l'intervale 0 et n
id1= tabEtudCopy.get(nb); // Si id1 retenu, l'etudiant sera selectionné
clobalId = id1; //clobalId est un string car c'est id1 declaré comme variable clobale
idRe = recherche(tabEtudInc,clobalId); // id1 dans liste d'incompatibilié ?
if(idRe == "-1") {
// Si OUI alors il est ajouté
tabTirage[0][0] = id1; // Il est rangé dans le tirage
tabEtudCopy.remove(nb); // Oté du tableau pour ne pas retomber sur le même
n1 = tabEtudCopy.size();
nb = random.nextInt(n1);
id2 = tabEtudCopy.get(nb);
tabTirage[0][1] = id2; // Son binomme est aussi tiré
tabEtudCopy.remove(nb);
Ajouter = true; // Pour sortir du while, aller dans la chambre suivante
}
else {
// Sinon alors on cherche le second et on vérifie s'il sont incompatibles
inRe = recherche2(tabEtudInc,clobalId); // avant on cherche son indice d'incompatibilié ?
Indice =inRe;
tabEtudCopy.remove(nb); // Evidemment il faut supprimer id1 avant
n1 = tabEtudCopy.size();
// System.out.println("n1: "+n1);
nb = random.nextInt(n1);
// System.out.println("nb: "+nb);
id2 = tabEtudCopy.get(nb);
// System.out.println("id2: "+id2);
if(((id1.equals(tabEtudInc[Indice][0])) && (!id2.equals(tabEtudInc[Indice][1])))
|| ((id1.equals(tabEtudInc[Indice][1])) && (!id2.equals(tabEtudInc[Indice][0])))) {
// Si le couplage est possible alors on les tire tous les deux
tabTirage[0][0] = id1;
tabTirage[0][1] = id2;
tabEtudCopy.remove(nb);
Ajouter = true;
}
else
tabEtudCopy.add(id1);
/* Si le duo n'est pas possible, on remet dans la table tabEtudCopy l'id1 qui a
été supprimé pour recommencer sans changer de chambre. Ajouter reste false */
}
}
} while(Ajouter == false);
// Les deux tirés sont rangés dans la selection
n1 = tabEtudSelectCopy.size();
tabEtudSelectCopy.add(tabTirage[0][0]);
tabEtudSelectCopy.add(tabTirage[0][1]);
}
/* récupére la selection en tableau static si le travail est fini
et tabEtudSelectCopy aura une taille = 2*c */
int j = 0;
for(String elem:tabEtudSelectCopy) { // Tiens on repasse de c à m !
tabEtudSelect[j][0] = elem; // Récupération
System.out.println("tabEtudSelect: "+elem);
j++;
}
}
}
//______________________________________________________________________________________________
public static String recherche(String [][]tabEtudInc, String clobalId) {
int n = tabEtudInc.length;
String sortietest="-1"; // cherche les incompatibilités, sort avec "-1" si id n'existe pas
loop:
for(int i = 0; i < n ; i++) {
for(int j = 0; j < 2; j++ ) {
if(clobalId.equals(tabEtudInc[i][j])) {
sortietest = tabEtudInc[i][j];
break loop;
}
}
}
return sortietest ;
}
public static int recherche2(String [][]tabEtudInc, String clobalId) {
int n = tabEtudInc.length;
int Indice=0; // cherche son indice dans la table incompatibilité
loop:
for(int i = 0; i < n ; i++) {
for(int j = 0; j < 2; j++ ) {
if(clobalId.equals(tabEtudInc[i][j])) {
Indice = i;
break loop;
}
}
}
return Indice ;
}
} |