用2个二维数组完成变换后的矩阵的修改

23级邓振宇2023024252 发表于 1年前 · 关联问题 Matrix

import java.util.Scanner;

public class Main {

public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int m = input.nextInt(); int type; int arr[][] = new int[n][n]; int arr2[][] = new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { arr[i][j]=input.nextInt();//矩阵的元素 } } for(int k=0;k<m;k++) { type = input.nextInt();//变换的型号 if (type == 1) { //变换1 //打印矩阵 for (int i = 0; i < n; i++) { for (int j = n - 1,t=0; j >= 0; j--,t++) { System.out.print(arr[j][i] + " "); arr2[i][t]=arr[j][i]; } System.out.println(); } System.out.println(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ arr[i][j] = arr2[i][j]; } } } else if (type == 2) { //变换2 //打印矩阵 for (int i = n - 1,t=0; i >= 0; i--,t++) { for (int j = 0; j < n; j++) { System.out.print(arr[j][i] + " "); arr2[t][j]=arr[j][i]; } System.out.println(); } System.out.println(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ arr[i][j] = arr2[i][j]; } } } else if (type == 3) {

//[x1][y1]关于[a][b]对称的点[x2][y2]满足x1+x2=2a,y1+y2=2b //变换3 //打印矩阵 for (int i = 0; i <n; i++) { for (int j = 0; j < n; j++) { System.out.print(arr[(n/2)*2-i][(n/2)*2-j]+" "); arr2[i][j]=arr[(n/2)*2-i][(n/2)*2-j]; } System.out.println(); } System.out.println(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ arr[i][j] = arr2[i][j]; } } } }

}

}