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.