Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author ArCiGo
- */
- public class Problema2 {
- public static void main(String[] args) throws FileNotFoundException {
- File f = null;
- FileReader fr = null;
- BufferedReader br = null;
- int fil = 0, col = 0;
- int matrixA[][], matrixB[][];
- try {
- f = new File("in.txt");
- fr = new FileReader(f);
- br = new BufferedReader(fr);
- String[] line = br.readLine().split(" ");
- fil = Integer.parseInt(line[0]);
- col = Integer.parseInt(line[1]);
- matrixA = new int[fil][col];
- matrixB = new int[fil][col];
- for (int i = 0; i < fil; i++) {
- String data[] = br.readLine().split(" ");
- for (int j = 0; j < data.length; j++) {
- matrixA[i][j] = Integer.parseInt(data[j]);
- }
- }
- for (int i = 0; i < fil; i++) {
- String data[] = br.readLine().split(" ");
- for (int j = 0; j < data.length; j++) {
- matrixB[i][j] = Integer.parseInt(data[j]);
- }
- }
- producto(matrixA, matrixB);
- fr.close();
- } catch (Exception ex) {
- System.err.println("Fail");
- }
- }
- public static int[][] producto(int matrixA[][], int matrixB[][]) throws IOException {
- File archivo = new File("out.txt");
- FileWriter fr = new FileWriter(archivo);
- BufferedWriter br = new BufferedWriter(fr);
- int matrixC[][] = new int[matrixA.length][matrixB.length];
- int acum = 0;
- for (int i = 0; i < matrixA.length; i++) {
- for (int j = 0; j < matrixB.length; j++) {
- acum = 0;
- for (int k = 0; k < matrixC.length; k++) {
- acum += (matrixA[i][k] * matrixB[k][j]);
- }
- matrixC[i][j] = acum;
- }
- }
- for (int i = 0; i < matrixA.length; i++) {
- for (int j = 0; j < matrixC.length; j++) {
- // System.out.print(matrixC[i][j]+" ");
- br.write(matrixC[i][j]+" ");
- }
- // System.out.println("");
- br.newLine();
- }
- br.close();
- return matrixC;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement