Advertisement
ArCiGo

Problema 2

Jul 20th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. /*
  6.  * To change this template, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9. /**
  10.  *
  11.  * @author ArCiGo
  12.  */
  13. public class Problema2 {
  14.  
  15.     public static void main(String[] args) throws FileNotFoundException {
  16.  
  17.         File f = null;
  18.         FileReader fr = null;
  19.         BufferedReader br = null;
  20.  
  21.         int fil = 0, col = 0;
  22.         int matrixA[][], matrixB[][];
  23.  
  24.         try {
  25.             f = new File("in.txt");
  26.             fr = new FileReader(f);
  27.             br = new BufferedReader(fr);
  28.  
  29.             String[] line = br.readLine().split(" ");
  30.             fil = Integer.parseInt(line[0]);
  31.             col = Integer.parseInt(line[1]);
  32.             matrixA = new int[fil][col];
  33.             matrixB = new int[fil][col];
  34.  
  35.             for (int i = 0; i < fil; i++) {
  36.                 String data[] = br.readLine().split(" ");
  37.                 for (int j = 0; j < data.length; j++) {
  38.                     matrixA[i][j] = Integer.parseInt(data[j]);
  39.                 }
  40.             }
  41.  
  42.             for (int i = 0; i < fil; i++) {
  43.                 String data[] = br.readLine().split(" ");
  44.                 for (int j = 0; j < data.length; j++) {
  45.                     matrixB[i][j] = Integer.parseInt(data[j]);
  46.                 }
  47.             }
  48.  
  49.             producto(matrixA, matrixB);
  50.            
  51.             fr.close();
  52.         } catch (Exception ex) {
  53.             System.err.println("Fail");
  54.         }
  55.  
  56.     }
  57.  
  58.     public static int[][] producto(int matrixA[][], int matrixB[][]) throws IOException {
  59.        
  60.         File archivo = new File("out.txt");
  61.         FileWriter fr = new FileWriter(archivo);
  62.         BufferedWriter br = new BufferedWriter(fr);
  63.  
  64.         int matrixC[][] = new int[matrixA.length][matrixB.length];
  65.         int acum = 0;
  66.  
  67.         for (int i = 0; i < matrixA.length; i++) {
  68.             for (int j = 0; j < matrixB.length; j++) {
  69.                 acum = 0;
  70.                 for (int k = 0; k < matrixC.length; k++) {
  71.                     acum += (matrixA[i][k] * matrixB[k][j]);
  72.                 }
  73.                 matrixC[i][j] = acum;
  74.             }
  75.         }
  76.        
  77.         for (int i = 0; i < matrixA.length; i++) {
  78.             for (int j = 0; j < matrixC.length; j++) {
  79. //                System.out.print(matrixC[i][j]+" ");
  80.                 br.write(matrixC[i][j]+" ");
  81.             }
  82. //            System.out.println("");
  83.             br.newLine();
  84.         }
  85.         br.close();
  86.         return matrixC;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement