Rezultati

Up. imeNalogaJezikRezultatČas oddaje
ayy-2018 Kvadrat števil C++ 100/100OK 13. okt '18 @ 12:00

Test Točke Porabljen spomin Porabljen čas Status
#1 10/10 3,023 MiB 0,000 s OK
#2 10/10 3,184 MiB 0,000 s OK
#3 10/10 3,188 MiB 0,004 s OK
#4 10/10 3,184 MiB 0,004 s OK
#5 10/10 3,195 MiB 0,003 s OK
#6 10/10 3,199 MiB 0,009 s OK
#7 10/10 3,277 MiB 0,019 s OK
#8 10/10 3,125 MiB 0,013 s OK
#9 10/10 3,281 MiB 0,025 s OK
#10 10/10 3,137 MiB 0,013 s OK

Ocenjevani program (main.cpp):
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool comparator (const int& a, const int& b) {
    return a > b;
}

int main() {
    int n, tmp;
    cin >> n;
    int n2 = n * n;

    vector<int> numbers;

    while (n2--) {
        cin >> tmp;
        numbers.push_back(tmp);
    }

    sort(numbers.begin(), numbers.end(), comparator);

    int cx, cy = cx = n / 2;
    vector<vector<int>> matrix(n, vector<int>(n));
    int index = 0;

    for (int level = 0; level < n; level++) {
        if (level == 0) {
            matrix[cy][cx] = numbers[index++];
        } else {
            int x = cx + level, y = cy;

            for (int i = 0; i < level; i++) {
                if (x >= n || y >= n || x < 0 || y < 0) {
                    x--; y--;
                    continue;
                }
                matrix[y][x] = numbers[index++];
                x--; y--;
            }

            for (int i = 0; i < level; i++) {
                if (x >= n || y >= n || x < 0 || y < 0) {
                    x--; y++;
                    continue;
                }
                matrix[y][x] = numbers[index++];
                x--; y++;
            }

            for (int i = 0; i < level; i++) {
                if (x >= n || y >= n || x < 0 || y < 0) {
                    x++; y++;
                    continue;
                }
                matrix[y][x] = numbers[index++];
                x++; y++;
            }

            for (int i = 0; i < level; i++) {
                if (x >= n || y >= n || x < 0 || y < 0) {
                    x++; y--;
                    continue;
                }
                matrix[y][x] = numbers[index++];
                x++; y--;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}