Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.0 kB
4
Indexable
Never
#include <iostream>
#include <cmath>

using namespace std;

void Beolvas(int &n)
{
    cout << "n = ";
    cin >> n;
}

void spiralSorrend(const int *matrix, int n, int *&spiral)
{
    int k = 0;
    int hossz = sqrt(n);
    int index = sqrt(n) - 1;

    while(hossz > 0)
    {
        for(int i = 1; i < hossz; i++)
        {
            spiral[k] = matrix[index];
            k++;
            index--;
        }

        for(int i = 1; i < hossz; i++)
        {
            spiral[k] = matrix[index];
            k++;
            index += sqrt(n);
        }

        for(int i = 1; i < hossz; i++)
        {
            spiral[k] = matrix[index];
            k++;
            index++;
        }

        for(int i = 1; i < hossz; i++)
        {
            spiral[k] = matrix[index];
            k++;
            index -= sqrt(n);
        }

        if(hossz == 1)
        {
            spiral[k] = matrix[index];
        }


        index += sqrt(n) - 1;
        hossz -= 2;
    }
}

void Kiir_Matrix(int n, const int *matrix)
{
    cout << "A matrix:" << endl;
    int gyok = sqrt(n);
    for(int i = 0; i < n; i++)
    {
        cout << matrix[i] << " ";
        if(n % gyok == 0)
        {
            cout << endl;
        }
    }
}

void Kiir_Spiral(int n, int *a)
{
    for(int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
}

int main()
{
    int n;
    Beolvas(n);

    const int *matrix = new int[n]{ 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 };
    //const int matrix[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    //const int matrix[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    //const int matrix[4] = { 1, 2, 3, 4 };
    //const int matrix[1] = { 1 };

    Kiir_Matrix(n, matrix);

    int *spiral = new int[n];
    spiralSorrend(matrix, n, spiral);
    Kiir_Spiral(n, spiral);

    delete[] matrix;
    delete[] spiral;

    return 0;
}