-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_PrintMatrix.cpp
More file actions
73 lines (66 loc) · 1.38 KB
/
Copy path20_PrintMatrix.cpp
File metadata and controls
73 lines (66 loc) · 1.38 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
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;
void PrintMatrixClockwisely(int** nums, int cols, int rows)
{
if (nums == NULL || cols <= 0 || rows <= 0)
return;
int iter_nums = cols;
if (rows < cols)
iter_nums = rows;
iter_nums = (iter_nums + 1)/2;
int k = 1;
while (k <= iter_nums)
{
int i = k-1;
int j = k-1;
while (j <= (cols-k))
{
cout << nums[i][j] << " ";
j++;
}
j--;
i++;
while(i <= rows-k)
{
cout << nums[i][j] << " ";
i++;
}
i--;
j--;
while (j >= k-1)
{
cout << nums[i][j] << " ";
j--;
}
j++;
i--;
while (i >= k)
{
cout << nums[i][j] << " ";
i--;
}
k++;
}
cout << endl;
}
int main()
{
int k;
int** nums1 = new int* [2];
for (int i = 0; i < 2; i++)
nums1[i] = new int[3];
k = 1;
for (int i = 0; i < 2;i++)
for (int j = 0;j < 3;j++)
nums1[i][j] = k++;
PrintMatrixClockwisely(nums1, 3, 2);
int** nums2 = new int* [4];
for (int i = 0; i < 4;i++)
nums2[i] = new int[4];
k = 1;
for (int i = 0; i < 4;i++)
for (int j = 0; j < 4;j++)
nums2[i][j] = k++;
PrintMatrixClockwisely(nums2, 4, 4);
return 0;
}