行列要素を回転させるJavaプログラム
この記事では、行列要素を回転させる方法を理解します。マトリックスは、行と列の要素を表したものです。マトリックスの回転とは、マトリックスの各要素の位置を右または左に1つシフトすることです。
以下は同じのデモンストレーションです-
入力がであると仮定します −
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
アルゴリズム
Step 1 - START Step 2 - Declare an integer matrix namely input_matrix and declare four integer values namely row, column, previous, next. Step 3 - Define the values. Step 4 - Iterate over each element of the matrix using a while loop and shift the position of each element by one position to the right using multiple for-loops and store the matrix. Step 5 - Display the result Step 5 - Stop
例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("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 input_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"); } } }
出力
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
例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("\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
-
反復クイックソート用のJavaプログラム
以下は、反復クイックソート用のJavaプログラムです- 例 public class Demo{ void swap_vals(int arr[], int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } int partition(int arr[], int l, int h){ &
-
配列ローテーション用のJavaプログラム
以下は、配列ローテーション用のJavaプログラムです- 例 public class Demo{ void rotate_left(int my_arr[], int d, int len){ d = d % len; int i, j, k, temp; int divisor = greatest_Common_divisor(d, len); for (i = 0; i < divisor;