Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để xác định xem một ma trận đã cho có phải là một ma trận thưa thớt hay không

Trong bài này, chúng ta sẽ hiểu cách xác định một Ma trận đã cho có phải là một ma trận thưa thớt hay không. Ma trận được cho là ma trận thưa nếu hầu hết các phần tử của ma trận đó bằng 0. Điều đó ngụ ý rằng nó chứa rất ít phần tử khác 0.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Input matrix:
4 0 6
0 0 9
6 0 0

Đầu ra mong muốn sẽ là -

Yes, the matrix is a sparse matrix

Thuật toán

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops, count the number of elements that have the value 0.
Step 5 - If the zero elements is greater than half the total elements, It’s a sparse matrix, else its not.
Step 6 - Display the result.
Step 7 - Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

public class Sparse {
   public static void main(String args[]) {
      int input_matrix[][] = {
         { 4, 0, 6 },
         { 0, 0, 9 },
         { 6, 0, 0 }
      };
      System.out.println("The matrix is defined as: ");
      int rows = 3;
      int column = 3;
      int counter = 0;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < column; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      for (int i = 0; i < rows; ++i)
         for (int j = 0; j < column; ++j)
            if (input_matrix[i][j] == 0)
               ++counter;
            if (counter > ((rows * column) / 2))
               System.out.println("\nYes, the matrix is a sparse matrix");
            else
               System.out.println("\nNo, the matrix is not a sparse matrix");
   }
}

Đầu ra

The matrix is defined as:
4 0 6
0 0 9
6 0 0

Yes, the matrix is a sparse matrix

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

public class Sparse {
   static int rows = 3;
   static int column = 3;
   static void is_sparse(int input_matrix[][]){
      int counter = 0;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < column; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      for (int i = 0; i < rows; ++i)
         for (int j = 0; j < column; ++j)
            if (input_matrix[i][j] == 0)
               ++counter;
            if (counter > ((rows * column) / 2))
               System.out.println("\nYes, the matrix is a sparse matrix");
            else
               System.out.println("\nNo, the matrix is not a sparse matrix");
   }
   public static void main(String args[]) {
      int input_matrix[][] = { { 4, 0, 6 },
         { 0, 0, 9 },
         { 6, 0, 0 }
      };
      System.out.println("The matrix is defined as: ");
      is_sparse(input_matrix);
   }
}

Đầu ra

The matrix is defined as:
4 0 6
0 0 9
6 0 0

Yes, the matrix is a sparse matrix