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

Chương trình Java để trao đổi các đường chéo

Trong bài này, chúng ta sẽ hiểu làm thế nào để trao đổi các đường chéo. Ma trận có sự sắp xếp theo hàng và cột của các phần tử của nó. Ma trận có m hàng và n cột có thể được gọi là ma trận m × n.

Các mục riêng lẻ trong ma trận được gọi là phần tử và có thể được biểu diễn bằng [i] [j], điều này gợi ý rằng phần tử a có trong hàng thứ i và cột thứ j.

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à -

The matrix is defined as:
4 5 6
1 2 3
7 8 9

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

The matrix after interchanging the elements:
6 5 4
1 2 3
9 8 7

Thuật toán

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix, and two integer value namely matrix_size and temp.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using multiple for-loops and swap the required elements of the matrix using a temporary variable.
Step 5 - Display the result
Step 5 - 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 InterchangeDiagonals {
   public static int matrix_size = 3;
   public static void main (String[] args) {
      int input_matrix[][] = {
         {4, 5, 6},
         {1, 2, 3},
         {7, 8, 9}
      };
      System.out.println("The matrix is defined as: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      for (int i = 0; i < matrix_size; ++i)
         if (i != matrix_size / 2) {
            int temp = input_matrix[i][i];
            input_matrix[i][i] = input_matrix[i][matrix_size - i - 1];
            input_matrix[i][matrix_size - i - 1] = temp;
         }
         System.out.println("\nThe matrix after interchanging the elements: ");
         for (int i = 0; i < matrix_size; ++i) {
            for (int j = 0; j < matrix_size; ++j)
               System.out.print(input_matrix[i][j]+" ");
            System.out.println();
         }
      }
}

Đầu ra

The matrix is defined as:
4 5 6
1 2 3
7 8 9

The matrix after interchanging the elements:
6 5 4
1 2 3
9 8 7

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 InterchangeDiagonals {
   public static int matrix_size = 3;
   static void interchange_diagonals(int input_matrix[][]) {
      for (int i = 0; i < matrix_size; ++i)
      if (i != matrix_size / 2) {
         int temp = input_matrix[i][i];
         input_matrix[i][i] = input_matrix[i][matrix_size - i - 1];
         input_matrix[i][matrix_size - i - 1] = temp;
      }
      System.out.println("\nThe matrix after interchanging the elements: ");
      for (int i = 0; i < matrix_size; ++i) {
         for (int j = 0; j < matrix_size; ++j)
         System.out.print(input_matrix[i][j]+" ");
         System.out.println();
      }
   }
   public static void main (String[] args) {
      int input_matrix[][] = {
         {4, 5, 6},
         {1, 2, 3},
         {7, 8, 9}
      };
      System.out.println("The matrix is defined as: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      interchange_diagonals(input_matrix);
   }
}

Đầu ra

The matrix is defined as:
4 5 6
1 2 3
7 8 9

The matrix after interchanging the elements:
6 5 4
1 2 3
9 8 7