jueves, 9 de diciembre de 2010

multiplicacion de 2 matrices

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package matriz;
import java.io.*;
import java.util.*;

/**
*
* @author xxx
*/
public class multiplicamatriz {
public static void main(String[] args) throws FileNotFoundException, IOException {

Scanner sn = new Scanner (new File ("in1.txt"));
FileOutputStream fos = new FileOutputStream ("out1.txt");
PrintStream ps = new PrintStream (fos);

int n = sn.nextInt();
int a [ ] [ ] = new int [n] [n];
int b [ ] [ ] = new int [n] [n];
int c [ ] [ ] = new int [n] [n];

for(int i=0;i<=n-1;i++){
for(int j=0;j<=n-1;j++){

a [i] [j] = sn.nextInt();
//ps.print(a [i] [j] + " ");

}
//ps.println();
}
for(int i=0;i<=n-1;i++){
for(int j=0;j<=n-1;j++){

b [i] [j] = sn.nextInt();
// ps.print(b [i] [j] + " ");
}
//ps.println();
}
for (int i=0;i<=n-1;i++){
for (int j=0;j<=n-1;j++){
c [i] [j]=0;
for (int k=0;k<=n-1;k++){
c [i] [j]+= a [i] [k] * b [k] [j];

}
}
}
for (int i=0;i<=n-1;i++){
for (int j=0;j<=n-1;j++){
System.out.print (c [i] [j] + " ");
ps.print(c [i][j] + " ");
}
ps.println();
System.out.println();
}

}
}




//multiplicacion de 2 matrices

No hay comentarios:

Publicar un comentario