/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2005 */ import java.io.*; public class ProduitMatriceMatrice { static BufferedReader flux = new BufferedReader(new InputStreamReader(System.in)); /* Fonction de calcul de la matrice produit */ /* de deux matrices carrees */ /* m1,m2 : Tableau d'entier */ /* Resultat fonction : Tableau d'entiers */ public static int [][] produitMatriceMatrice(int [][] m1,int [][] m2) { /* Allocation de la matrice */ int n = m1.length; int [][] m = new int[n][n]; /* Pour toutes les lignes de la matrice a calculer */ for ( int i = 0 ; i < n ; i++ ) /* Pour toutes les colonnes de la matrice a calculer */ for ( int j = 0 ; j < n ; j++ ) { /* Produit ligne par colonne */ m[i][j] = 0; for ( int k = 0 ; k < n ; k++ ) m[i][j] += m1[i][k]*m2[k][j]; } /* Resultat de la fonction */ return(m); } /* Fonction de creation d'une matrice d'entiers */ /* initialisee avec des nombres aleatoires */ /* l,c : nombres de lignes et de colonnes de la matrice */ /* max : valeur maximale des nombres aleatoires */ /* Resultat fonction : Matrice d'entiers */ public static int [][] creationMatriceAleatoire(int l,int c,int max) { /* Allocation de la matrice */ int [][] m = new int[l][c]; /* Pour toutes les lignes de la matrice a initialiser */ for ( int i = 0 ; i < l ; i++ ) /* Pour toutes les colonnes de la matrice a initialiser */ for ( int j = 0 ; j < c ; j++ ) { /* Initialisation avec un nombre aleatoire compris */ /* entre 0 et max */ m[i][j] =(int) (Math.random()*(max+1)); } /* Resultat de la fonction */ return(m); } public static String justifieADroite(int v,int larg) { /* Definition et initialisation de la chaine */ /* avec l'entier a formater */ String s = ""+v; /* Ajout a gauche a la chaine d'autant de caracteres */ /* espace qu'il le faut pour que sa longueur devienne */ /* superieure ou egale a la largeur recherchee */ while ( s.length() < larg ) s = " "+s; /* Resultat de la fonction */ return(s); } /* Fonction d'affichage d'une matrice d'entiers */ /* m : tableau d'entiers a afficher */ /* larg : largeur de chaque colonne */ public static void affichageMatrice(int [][] m,int larg) { for ( int i = 0 ; i < m.length ; i++ ) { /* Pour toute les lignes de la matrice a afficher */ for ( int j = 0 ; j < m[0].length ; j++ ) { /* Affichage d'un entier de la ligne i */ /* (sans aller a la ligne */ System.out.print(justifieADroite(m[i][j],larg+1)); } /* On va a la ligne quand tous les entiers d'une ligne */ /* ont ete affiches */ System.out.println(); } } /* Fonction principale */ public static void main(String [] args) throws IOException { /* Lecture au clavier du nombre de lignes */ /* et du nombre de colonnes de la matrice */ System.out.print("Taille des matrices : "); int n = Integer.valueOf(flux.readLine()).intValue(); System.out.print("Valeur maximale : "); int max = Integer.valueOf(flux.readLine()).intValue(); /* Creation de deux matrices de n sur n entiers */ int [][] m1 = creationMatriceAleatoire(n,n,max); int [][] m2 = creationMatriceAleatoire(n,n,max); /* Affichage des deux matrices */ System.out.println("1ere matrice : "); affichageMatrice(m1,(""+max).length()); System.out.println("2eme matrice : "); affichageMatrice(m2,(""+max).length()); /* Calcul et affichage du produit des deux matrices */ int [][] m = produitMatriceMatrice(m1,m2); System.out.println("Produit : "); affichageMatrice(m,(""+max).length()*2); } }