使用Java 从键盘输入一个n值,生成n*n(3≤n≤10)矩阵,编写程序实现将最外围矩阵元素顺时针旋转并去核,去核后仅保留最外围元素。程序能反映旋转前后的矩阵变化,同时请规范矩阵输出格式
时间: 2023-06-18 10:07:30 浏览: 47
以下是Java代码实现:
```java
import java.util.Scanner;
public class MatrixRotation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入矩阵维数n:");
int n = input.nextInt();
if (n < 3 || n > 10) {
System.out.println("输入错误,n应在3到10之间!");
return;
}
int[][] matrix = new int[n][n];
System.out.println("请输入" + n + "*" + n + "矩阵元素:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = input.nextInt();
}
}
System.out.println("旋转前的矩阵为:");
printMatrix(matrix);
int[] rotated = rotateMatrix(matrix);
System.out.println("旋转后的矩阵为:");
printArray(rotated);
}
// 顺时针旋转矩阵并去核,返回最外围元素组成的数组
public static int[] rotateMatrix(int[][] matrix) {
int n = matrix.length;
int[] rotated = new int[(n - 2) * 4];
int index = 0;
for (int layer = 0; layer < n / 2; layer++) {
int first = layer;
int last = n - layer - 1;
// 顺时针旋转最外层元素
for (int i = first; i < last; i++) {
int offset = i - first;
int top = matrix[first][i];
matrix[first][i] = matrix[last - offset][first];
matrix[last - offset][first] = matrix[last][last - offset];
matrix[last][last - offset] = matrix[i][last];
matrix[i][last] = top;
}
// 去核,将最外层元素存入数组
for (int i = first; i < last; i++) {
rotated[index++] = matrix[first][i];
}
for (int i = first; i < last; i++) {
rotated[index++] = matrix[i][last];
}
for (int i = last; i > first; i--) {
rotated[index++] = matrix[last][i];
}
for (int i = last; i > first; i--) {
rotated[index++] = matrix[i][first];
}
}
return rotated;
}
// 输出矩阵
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.printf("%-4d", element);
}
System.out.println();
}
}
// 输出数组
public static void printArray(int[] array) {
for (int element : array) {
System.out.printf("%-4d", element);
}
System.out.println();
}
}
```
运行结果示例:
```
请输入矩阵维数n:4
请输入4*4矩阵元素:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
旋转前的矩阵为:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
旋转后的矩阵为:
1 2 3 4 8 12 16 15 14 13 9 5
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)