Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java in phần tử biên của ma trận

Giới thiệu

Trong bài viết này, chúng ta sẽ tìm hiểu cách in các phần tử biên (boundary elements) của ma trận. Ma trận là biểu diễn các phần tử theo hàng và cột. Phần tử biên là những phần tử không bị bao quanh bởi các phần tử khác ở 4 hướng. Ví dụ: phần tử ở hàng đầu, cột đầu, hàng cuối, cột cuối.

Minh họa

Đầu vào:

The input matrix: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5

Đầu ra mong muốn:

The border elements of the matrix is: 9 8 9 8 8   7 7   6 6 5 6 5

Thuật toán

  1. Bắt đầu
  2. Khai báo: ma trận input_matrix, đối tượng BoundaryElements tên border_values
  3. Khởi tạo giá trị
  4. Duyệt ma trận bằng 2 vòng lặp for, kiểm tra phần tử biên bằng điều kiện OR: hàng 0, cột 0, hàng cuối, cột cuối
  5. Hiển thị các phần tử biên
  6. Kết thúc

Ví dụ 1: Nhập từ bàn phím

public class BoundaryElements {
    public static void main(String[] args) {
        int input_matrix[][] = new int[][] {
            { 9, 8, 9, 8 },
            { 8, 7, 8, 7 },
            { 7, 6, 7, 6 },
            { 6, 5, 6, 5 }
        };
        
        System.out.println("The matrix is defined as: ");
        for (int x = 0; x < input_matrix.length; x++) {
            for (int y = 0; y < input_matrix[x].length; y++) {
                System.out.print(input_matrix[x][y] + " ");
            }
            System.out.println();
        }
        
        BoundaryElements border_values = new BoundaryElements();
        System.out.println("The border elements of the matrix is:");
        
        for (int x = 0; x < input_matrix.length; x++) {
            for (int y = 0; y < input_matrix[x].length; y++) {
                if (x == 0 || y == 0 || x == input_matrix.length - 1 || y == input_matrix[x].length - 1) {
                    System.out.print(input_matrix[x][y] + " ");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Đầu ra

The matrix is defined as: 
9 8 9 8 
8 7 8 7 
7 6 7 6 
6 5 6 5 
The border elements of the matrix is:
9 8 9 8 
8     7 
7     6 
6 5 6 5

Ví dụ 2: Tách phương thức instance (OOP)

public class BoundryElements {
    public void Boundary_Elements(int input_matrix[][]) {
        System.out.println("The matrix is defined as: ");
        for (int x = 0; x < input_matrix.length; x++) {
            for (int y = 0; y < input_matrix[x].length; y++) {
                System.out.print(input_matrix[x][y] + " ");
            }
            System.out.println();
        }
        
        System.out.println("The border elements of the matrix is:");
        for (int x = 0; x < input_matrix.length; x++) {
            for (int y = 0; y < input_matrix[x].length; y++) {
                if (x == 0 || y == 0 || x == input_matrix.length - 1 || y == input_matrix[x].length - 1) {
                    System.out.print(input_matrix[x][y] + " ");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        int input_matrix[][] = new int[][] {
            { 9, 8, 9, 8 },
            { 8, 7, 8, 7 },
            { 7, 6, 7, 6 },
            { 6, 5, 6, 5 }
        };
        
        BoundryElements border_values = new BoundryElements();
        border_values.Boundary_Elements(input_matrix);
    }
}

Đầu ra

The matrix is defined as: 
9 8 9 8 
8 7 8 7 
7 6 7 6 
6 5 6 5 
The border elements of the matrix is:
9 8 9 8 
8     7 
7     6 
6 5 6 5