Java 程序旋转矩阵元素

javacampus interviewserver side programmingprogramming

在本文中,我们将了解如何旋转矩阵元素。矩阵是行和列中元素的表示。矩阵旋转是将矩阵中每个元素的位置向右或向左移动 1 个位置。

下面是相同的演示 −

假设我们的输入是

矩阵定义为
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

期望输出的将是

一次旋转后的矩阵:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

算法

步骤 1 - 开始
步骤 2 - 声明一个整数矩阵,即 input_matrix,并声明四个整数值,即 row、column、previous、next。
步骤 3 - 定义值。
步骤 4 - 使用 while 循环遍历矩阵的每个元素,并使用多个 for 循环将每个元素的位置向右移动一个位置并存储矩阵。
步骤 5 - 显示结果
步骤 5 - 停止

示例 1

此处,输入由用户根据提示输入。

public class RotateMatrix {
   static int Rows = 4;
   static int Columns = 4;
   public static void main(String[] args) {
      int input_matrix[][] = {
         {1, 2, 3, 4},
         {5, 6, 7, 8},
         {9, 10, 11, 12},
         {13, 14, 15, 16}
      };
      System.out.println("input_matrix 定义为 ");
      for (int i = 0; i < Rows; i++) {
         for (int j = 0; j < Columns; j++)
         System.out.print( input_matrix[i][j] + " ");
         System.out.print("\n");
      }
      int m = Rows, n = Columns;
      int row = 0, column = 0;
      int previous, current;
      while (row < m && column < n) {
         if (row + 1 == m || column + 1 == n)
            break;
         previous = input_matrix[row + 1][column];
         for (int i = column; i < n; i++) {
            current = input_matrix[row][i];
            input_matrix[row][i] = previous;
            previous = current;
         }
         row++;
         for (int i = row; i < m; i++) {
            current = input_matrix[i][n-1];
            input_matrix[i][n-1] = previous;
            previous = current;
         }
         n--;
         if (row < m) {
            for (int i = n-1; i >= column; i--) {
               current = input_matrix[m-1][i];
               input_matrix[m-1][i] = previous;
               previous = current;
            }
         }
         m--;
         if (column < n) {
            for (int i = m-1; i >= row; i--) {
               current = input_matrix[i][column];
               input_matrix[i][column] = previous;
               previous = current;
            }
         }
         column++;
      }
      System.out.println("\n旋转一圈后的 input_matrix: ");
      for (int i = 0; i < Rows; i++) {
         for (int j = 0; j < Columns; j++)
         System.out.print( input_matrix[i][j] + " ");
         System.out.print("\n");
      }
   }
}

输出

input_matrix 定义为
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

旋转一圈后的 input_matrix:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

示例 2

这里,整数已经被预先定义,并且它的值被访问并显示在控制台上。

public class RotateMatrix {
   static int Rows = 4;
   static int Columns = 4;
   static void Rotate_matrix(int m,
   int n, int matrix[][]) {
      int row = 0, column = 0;
      int previous, current;
      while (row < m && column < n) {
         if (row + 1 == m || column + 1 == n)
            break;
         previous = matrix[row + 1][column];
         for (int i = column; i < n; i++) {
            current = matrix[row][i];
            matrix[row][i] = previous;
            previous = current;
         }
         row++;
         for (int i = row; i < m; i++) {
            current = matrix[i][n-1];
            matrix[i][n-1] = previous;
            previous = current;
         }
         n--;
         if (row < m) {
            for (int i = n-1; i >= column; i--) {
               current = matrix[m-1][i];
               matrix[m-1][i] = previous;
               previous = current;
            }
         }
         m--;
         if (column < n) {
            for (int i = m-1; i >= row; i--) {
               current = matrix[i][column];
               matrix[i][column] = previous;
               previous = current;
            }
         }
         column++;
      }
      System.out.println("\n经过一次旋转后的矩阵: ");
      for (int i = 0; i < Rows; i++) {
         for (int j = 0; j < Columns; j++)
         System.out.print( matrix[i][j] + " ");
         System.out.print("\n");
      }
   }
   public static void main(String[] args) {
      int input_matrix[][] = {
         {1, 2, 3, 4},
         {5, 6, 7, 8},
         {9, 10, 11, 12},
         {13, 14, 15, 16}
      };
      System.out.println("矩阵定义为 ");
      for (int i = 0; i < Rows; i++) {
         for (int j = 0; j < Columns; j++)
         System.out.print( input_matrix[i][j] + " ");
         System.out.print("\n");
      }
      Rotate_matrix(Rows, Columns, input_matrix);
   }
}

输出

矩阵定义为
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

经过一次旋转后的矩阵:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

相关文章