Untitled
unknown
c_cpp
4 years ago
1.2 kB
12
Indexable
#include <iostream>
#include <cmath>
using namespace std;
void beOlvas(int& n)
{
cin >> n;
}
void kiIrMatrix(int n, const int* matrix)
{
for (int i = 0; i < n * n; ++i)
{
cout << matrix[i] << ' ';
if ((i + 1) % n == 0)
cout << '\n';
}
}
void spiralSorrend(const int* a, int n, int* b)
{
int N = sqrt(n * n);
//spiral tomb indexe
int l = 0;
//matrix tomb indexe
int ind = 0;
for (int i = N; i > 0;)
{
for (int j = 1; j < i; ++j)
b[l++] = a[ind++];
for (int j = 1; j < i; ++j)
b[l++] = a[ind], ind += sqrt(n * n);
for (int j = 1; j < i; ++j)
b[l++] = a[ind--];
for (int j = 1; j < i; ++j)
b[l++] = a[ind], ind -= sqrt(n * n);
if (i == 1)
b[l] = a[ind];
ind += sqrt(n * n) + 1;
i -= 2;
}
}
void kiIrSpiral(int n, int* a)
{
for (int i = 0; i < n * n; ++i)
cout << a[i] << ' ';
}
int main()
{
int n;
beOlvas(n);
const int* matrix = new int[n * n]{ 1, 2, 3, 4, 5, 6, 7, 8, 9};
int* spiral = new int[n * n];
kiIrMatrix(n, matrix);
spiralSorrend(matrix, n, spiral);
kiIrSpiral(n, spiral);
delete[] matrix;
delete[] spiral;
return 0;
}Editor is loading...