-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintMatrix.cpp
More file actions
63 lines (48 loc) · 1.24 KB
/
PrintMatrix.cpp
File metadata and controls
63 lines (48 loc) · 1.24 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
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
using namespace std;
static vector<vector<int>> matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
};
void printCircle( const vector<vector<int>> &matrix, int height, int width, int layer) {
//cout << "layer: " << layer << endl;
int endX = width - 1 - layer;
int endY = height - 1 - layer;
for( int i = layer; i <= endX; ++i) {
cout << matrix[layer][i] << " ";
}
if( layer < endY) {
for( int i = layer+1; i <= endY; ++i) {
cout << matrix[i][endX] << " ";
}
}
if( layer < endX && layer < endY) {
for( int i = endX - 1; i >= layer; --i) {
cout << matrix[endY][i] << " ";
}
}
if( layer < endX && layer < endY - 1) {
for( int i = endY-1; i >= layer+1; --i) {
cout << matrix[i][layer] << " ";
}
}
return;
}
void printMatrix( const vector<vector<int>> &matrix) {
if( matrix.size() == 0 || matrix[0].size() == 0) return;
int height = matrix.size();
int width = matrix[0].size();
int start = 0;
while( start*2 < height && start*2 < width ){
printCircle(matrix, height, width, start);
++start;
}
cout << endl;
}
int main(void) {
printMatrix(matrix);
return 0;
}