Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpublic class Matrice { public static int[][] GenerateMatrice (int n, int narc) { int M[][]; Random R = new Random(); int i , j ,cpt=0 ; M = new int[n][n]; for (i=0; i<n; i++) M[i]=new int[n]; for(i=0; i<n;i++) { for(j=0;j<n; j++) { if (i==j) M[i][j]=0; else M[i][j]=999999; }} for(i=0;i<n;i++) // pour s'assurer que chaque noeud est connecté à au moins un autre noeud {do{ j=Math.abs(R.nextInt(n)); }while((i==j)|| M[i][j]!=999999 ); M[i][j]= Math.abs(R.nextInt(99))+1; } for(cpt=0; cpt<(narc-n); cpt++) // remplir les arcs restants {do{ i=Math.abs(R.nextInt(n)); j=Math.abs(R.nextInt(n)); }while((i==j)|| M[i][j]!=999999 ); M[i][j]= Math.abs(R.nextInt(99)+1); } for(int k=0;k<n;k++) { for(int l=0;l<n;l++) { System.out.print(M[k][l]+"," + "\t");} System.out.println(); } return M ; }