public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<List<Integer>> matrix = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add(i * n + j + 1);
}
matrix.add(row);
}
print(matrix);
int m = scanner.nextInt();
matrix = rotate(matrix, m % 4);
print(matrix);
}
static void print(List<List<Integer>> matrix) {
for (List<Integer> row : matrix) {
for (Integer integer : row) {
System.out.print(integer + " ");
}
System.out.println();
}
}
static List<List<Integer>> rotate(List<List<Integer>> matrix, int m) {
if(m == 0){
return matrix;
}
List<List<Integer>> result = new ArrayList<>();
int size = matrix.size();
for (int i = 0; i < size; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < size; j++) {
row.add(matrix.get(size - j - 1).get(i));
}
result.add(row);
}
return rotate(result, m - 1);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
运行