Didart

Intersection of Two Matrices

Jan 3rd, 2023
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. package MultidimensionalArrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class IntersectionOfTwoMatrices {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int rows = Integer.parseInt(scanner.nextLine());
  10.         int cols = Integer.parseInt(scanner.nextLine());
  11.  
  12.         char[][] firstMatrix = new char[rows][cols];
  13.         char[][] secondMatrix = new char[rows][cols];
  14.  
  15.         for (int row = 0; row < firstMatrix.length; row++) {
  16.             String[] line = scanner.nextLine().split(" ");
  17.             for (int col = 0; col < firstMatrix[0].length; col++) {
  18.                 firstMatrix[row][col] = line[col].charAt(0);
  19.             }
  20.         }
  21.         for (int row = 0; row < secondMatrix.length; row++) {
  22.             String[] line = scanner.nextLine().split(" ");
  23.             for (int col = 0; col < secondMatrix[0].length; col++) {
  24.                 secondMatrix[row][col] = line[col].charAt(0);
  25.             }
  26.         }
  27.         for (int row = 0; row < firstMatrix.length; row++) {
  28.             System.out.println();
  29.             for (int col = 0; col < firstMatrix[0].length; col++) {
  30.                 if (firstMatrix[row][col] != secondMatrix[row][col]) {
  31.                     System.out.print("* ");
  32.                 } else {
  33.                     System.out.print(firstMatrix[row][col] + " ");
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment