-
Notifications
You must be signed in to change notification settings - Fork 6
/
20_PrintMatrix.cpp
77 lines (64 loc) · 1.31 KB
/
20_PrintMatrix.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdio>
using namespace std;
#define MAX 101
int PrintMatrixInCircle(int (*num)[MAX], int row, int column, int start)
{
if (num == NULL || row <= 0 || column <= 0)
return 0;
int endx, endy, i;
endx = column - start - 1;
endy = row - start - 1;
//from left to right
for (i = start; i <= endx; i ++)
{
printf("%d ", num[start][i]);
}
//from up to down
if (endy > start)
{
for (i = start + 1; i <= endy; i ++)
printf("%d ", num[i][endx]);
}
//from right to left
if (endy > start && endx > start)
{
for (i = endx - 1; i >= start; i--)
printf("%d ", num[endy][i]);
}
//from down to up
if (endy > start + 1 && endx > start)
{
for (i = endy - 1; i > start; i --)
printf("%d ", num[i][start]);
}
return 0;
}
void PrintMatrixClockwisely(int (*num)[MAX], int row, int column)
{
if (num == NULL || row <= 0 || column <= 0)
return ;
int start = 0;
while (column > start * 2 && row > start * 2)
{
PrintMatrixInCircle(num, row, column, start);
++ start;
}
return ;
}
int main(void)
{
int num[MAX][MAX];
int m, n, i, j;
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
while (cin >> m >> n)
{
for (i = 0; i < m; i ++)
for (j = 0; j < n; j ++)
cin >> num[i][j];
PrintMatrixClockwisely(num, m, n);
cout << endl;
}
return 0;
}