-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
55 lines (48 loc) · 1.17 KB
/
Copy pathmain.cpp
File metadata and controls
55 lines (48 loc) · 1.17 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
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// заполнение матрици по спирали
int n, m;
cin >> n >> m;
int arr[n][m];
for (int i = 0; i < n ; i++) {
for (int j = 0; j < m ; j++) {
arr[i][j] = 0;
}
}
int number = 1, x = 0, y = 0;
while (number <= n*m) {
x++;
if (number <=n*m) {
for (int j = x - 1; j <= m - x; j++) {
arr[x - 1][j] = number++;
}
}
if (number <=n*m) {
for (int j = x; j <= n - x; j++) {
arr[j][m - y - 1] = number++;
}
}
y++;
if(number <=n*m) {
for (int j = m - y - 1; j >= x - 1; j--) {
arr[n - x][j] = number++;
}
}
if (number<=n*m) {
for (int j = n - x - 1; j >= x; j--) {
arr[j][x - 1] = number++;
}
}
}
cout << " ";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout.width(4);
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}