Advertisement
deyanmalinov

01. Compare Matrices

May 23rd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args){
  7.         Scanner scan = new Scanner(System.in);
  8.         String [] matrixSize = scan.nextLine().split(" ");
  9.         int row = Integer.parseInt(matrixSize[0]);
  10.         int col = Integer.parseInt(matrixSize[1]);
  11.  
  12.         int [][] matrix = new int[row][col];
  13.  
  14.         for (int i = 0; i < row; i++) {
  15.             String[] m1 = scan.nextLine().split(" ");
  16.             for (int j = 0; j < col; j++) {
  17.                 matrix[i][j] = Integer.parseInt(m1[j]);
  18.  
  19.             }
  20.         }
  21.  
  22.         String [] matrixSize2 = scan.nextLine().split(" ");
  23.         int row2 = Integer.parseInt(matrixSize2[0]);
  24.         int col2 = Integer.parseInt(matrixSize2[1]);
  25.  
  26.         int [][] matrix2 = new int[row2][col2];
  27.  
  28.         for (int i = 0; i < row2; i++) {
  29.             String[] m12 = scan.nextLine().split(" ");
  30.             for (int j = 0; j < col2; j++) {
  31.                 matrix2[i][j] = Integer.parseInt(m12[j]);
  32.  
  33.             }
  34.         }
  35.         if (EqualMatrix(matrix, matrix2)){
  36.             System.out.println("equal");
  37.  
  38.         }else {
  39.             System.out.println("not equal");
  40.         }
  41.  
  42.     }
  43.     public static boolean EqualMatrix(int[][] matrix, int[][] matrix2){
  44.         if (matrix.length != matrix2.length){
  45.             return false;
  46.         }
  47.         for (int i = 0; i < matrix.length; i++) {
  48.              if (matrix[i].length != matrix2[i].length){
  49.                  return false;
  50.              }
  51.             for (int j = 0; j < matrix[i].length; j++) {
  52.                 if (matrix[i][j] != matrix2[i][j]) {
  53.                     return false;
  54.                 }
  55.             }
  56.         }
  57.         return true;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement