Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lekciq;
- import java.util.Scanner;
- public class IntersectionOfTwoMatrices {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read the dimensions of the matrices
- int M = scanner.nextInt();
- int N = scanner.nextInt();
- // Read the first matrix
- char[][] A = readMatrix(scanner, M, N);
- // Read the second matrix
- char[][] B = readMatrix(scanner, M, N);
- // Create and fill the third matrix C with intersecting elements
- char[][] C = new char[M][N];
- for (int i = 0; i < M; i++) {
- for (int j = 0; j < N; j++) {
- if (A[i][j] == B[i][j]) {
- C[i][j] = A[i][j];
- } else {
- C[i][j] = '*';
- }
- }
- }
- // Print the third matrix C
- printMatrix(C);
- }
- // Method to read a matrix from the console
- private static char[][] readMatrix(Scanner scanner, int rows, int cols) {
- char[][] matrix = new char[rows][cols];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- matrix[i][j] = scanner.next().charAt(0);
- }
- }
- return matrix;
- }
- // Method to print a matrix
- private static void printMatrix(char[][] matrix) {
- for (char[] row : matrix) {
- for (char element : row) {
- System.out.print(element + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment