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

Chương trình Java hiển thị ma trận tam giác trên

Giới thiệu

Trong bài viết này, chúng ta sẽ tìm hiểu cách hiển thị ma trận tam giác trên (upper triangular matrix). Ma trận có sự sắp xếp hàng và cột. Ma trận m hàng n cột gọi là ma trận m × n. Ma trận tam giác trên là ma trận mà tất cả các phần tử dưới đường chéo chính đều bằng 0.

Minh họa

Đầu vào:

The matrix is defined as: 2 1 4 1 2 3 3 6 2

Đầu ra mong muốn:

The upper triangular matrix is: 2 1 4 0 2 3 0 0 2

Thuật toán

  1. Bắt đầu
  2. Khai báo ma trận số nguyên input_matrix
  3. Khởi tạo giá trị
  4. Duyệt từng phần tử bằng 2 vòng lặp for, gán 0 cho các vị trí [i][j] dưới đường chéo (i > j)
  5. Hiển thị ma trận kết quả
  6. Kết thúc

Ví dụ 1: Triển khai trong hàm main

public class UpperTriangle {
    public static void upper_triangular_matrix(int input_matrix[][]) {
    }
    
    public static void main(String[] args) {
        int input_matrix[][] = {
            { 2, 1, 4 },
            { 1, 2, 3 },
            { 3, 6, 2 }
        };
        
        int rows = input_matrix.length;
        int column = input_matrix[0].length;
        
        System.out.println("The matrix is defined as: ");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(input_matrix[i][j] + " ");
            }
            System.out.println();
        }
        
        if (rows != column) {
            return;
        } else {
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < column; j++) {
                    if (i > j) {
                        input_matrix[i][j] = 0;
                    }
                }
            }
            
            System.out.println("\nThe upper triangular matrix is: ");
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < column; j++) {
                    System.out.print(input_matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}

Đầu ra

The matrix is defined as: 
2 1 4 
1 2 3 
3 6 2 
The upper triangular matrix is: 
2 1 4 
0 2 3 
0 0 2

Ví dụ 2: Tách hàm static (OOP)

public class UpperTriangle {
    public static void upper_triangular_matrix(int input_matrix[][]) {
        int rows = input_matrix.length;
        int column = input_matrix[0].length;
        
        if (rows != column) {
            return;
        } else {
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < column; j++) {
                    if (i > j) {
                        input_matrix[i][j] = 0;
                    }
                }
            }
            
            System.out.println("\nThe upper triangular matrix is: ");
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < column; j++) {
                    System.out.print(input_matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
    
    public static void main(String[] args) {
        int input_matrix[][] = {
            { 2, 1, 4 },
            { 1, 2, 3 },
            { 3, 6, 2 }
        };
        
        int rows = input_matrix.length;
        int column = input_matrix[0].length;
        
        System.out.println("The matrix is defined as: ");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(input_matrix[i][j] + " ");
            }
            System.out.println();
        }
        
        upper_triangular_matrix(input_matrix);
    }
}

Đầu ra

The matrix is defined as: 
2 1 4 
1 2 3 
3 6 2 
The upper triangular matrix is: 
2 1 4 
0 2 3 
0 0 2