-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaTranXoanOc.cpp
35 lines (33 loc) · 925 Bytes
/
MaTranXoanOc.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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
int a[m][n];
int dong = m, cot = n, k = 1, p = 0, i, j;
while (k <= m * n)
{
for (i = p; i < cot; i++) /*Loop to access the first row of the array*/
a[p][i] = k++;
for (i = p + 1; i < dong; i++) /*Loop tp access the last column of the array*/
a[i][cot - 1] = k++;
for (i = cot - 2; i >= p; i--) /*Loop to access the last row of the array*/
a[dong - 1][i] = k++;
for (i = dong - 2; i > p; i--) /*Loop to access the first column of the array*/
a[i][p] = k++;
p++;
dong--;
cot--;
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cout << setw(5);
cout << a[i][j] << " ";
}
cout << endl;
}
}