Java 矩阵元素旋转程序


在本文中,我们将了解如何使用Java旋转矩阵元素。矩阵是元素在行和列中表示的一种方式。矩阵旋转是指将矩阵的每个元素的位置向右或向左移动 1 个位置。我们将使用两种方法:一种是根据用户输入旋转矩阵,另一种是在代码中定义矩阵。

问题陈述

编写一个 Java 程序来旋转矩阵元素。下面是演示:

输入

The matrix is defined as
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

输出

The matrix after one rotation:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

不同的方法

以下是旋转矩阵元素的不同方法:

使用用户输入

以下是使用基于用户的输入旋转矩阵元素的步骤:

  • 首先,我们将从java.util 包中导入Scanner 类
  • 从用户那里获取矩阵维数(行和列)的输入。
  • 要求用户逐行输入矩阵元素,并使用 m 和 n 表示行和列,以及 row 和 column 来跟踪当前位置。
  • 旋转过程
    • 从左到右沿第一行。
    • 从上到下沿最后一列。
    • 从右到左穿过最后一行。
    • 从下到上沿第一列。
  • 显示旋转后的矩阵并打印旋转一次后的矩阵。

示例

在给定的示例中,用户根据提示输入输入:

import java.util.Scanner;
public class RotateMatrix {
   static int Rows, Columns;
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the number of rows: ");
      Rows = sc.nextInt();
      System.out.print("Enter the number of columns: ");
      Columns = sc.nextInt();
      int input_matrix[][] = new int[Rows][Columns];
      System.out.println("Enter the elements of the matrix:");
      for (int i = 0; i < Rows; i++) {
         for (int j = 0; j < Columns; j++) {
            input_matrix[i][j] = sc.nextInt();
         }
      }
      System.out.println("The input matrix is defined as:");
      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("\nThe matrix after one rotation:");
      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");
      }
      sc.close();
   }
}

输出

The input_matrix is defined as
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

The input_matrix after one rotation:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

使用预定义输入

以下是使用预定义输入旋转矩阵元素的步骤:

  • 首先,我们将定义矩阵维数(行 = 4,列 = 4)。
  • 定义一个具有预定义值的矩阵并打印输入矩阵。
  • 调用Rotate_matrix() 方法,并传递矩阵及其维数。
  • Rotate_matrix() 方法内部:为 row、column 和 previous/current 变量设置初始值。
  • 使用while 循环旋转矩阵,只要矩阵没有缩减到单行或单列。
  • 按与第一个程序相同的顺序旋转元素。
  • 就地更新矩阵。
  • 循环结束后,打印旋转后的矩阵。

示例

这里,整数已预先定义,并在控制台上访问并显示其值:

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("\nThe matrix after one rotation: ");
      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("The matrix is defined as ");
      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);
   }
}

输出

The matrix is defined as
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

The matrix after one rotation:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

更新时间: 2024-10-16

679 次查看

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.