-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRotationMatrix02.cpp
More file actions
52 lines (37 loc) · 1.1 KB
/
RotationMatrix02.cpp
File metadata and controls
52 lines (37 loc) · 1.1 KB
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
50
51
52
#include<bits/stdc++.h>
using namespace std;
void RotationMatrix(int row, int column) {
int matrix[row][column];
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cin >> matrix[i][j];
}
}
for (int layer = 0; layer < row / 2; ++layer) {
int first = layer;
int last = row - 1 - layer;
for (int i = first; i < last; ++i) {
int offSet = i - first;
int top = matrix[first][i]; //save top
// left -> top
matrix[first][i] = matrix[last - offSet][first];
// bottom -> left
matrix[last - offSet][first] = matrix[last][last - offSet];
// right -> bottom
matrix[last][last - offSet] = matrix[i][last];
// top -> right
matrix[i][last] = top;
}
}
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int row, column;
cin >> row >> column;
RotationMatrix(row, column);
}