Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int* Get_Line(int** a, int k, int n) {
- int SIZE, i, j, ptr = 0;
- if (k < n) {
- SIZE = k + 1;
- i = k;
- j = 0;
- }
- else {
- SIZE = 2 * n - 1 - k;
- i = n - 1;
- j = k - n + 1;
- }
- int* res = new int[SIZE];
- while (i >= 0 && j < n)
- res[ptr++] = a[i--][j++];
- return res;
- }
- int** init(int n)
- {
- int** a = new int*[n];
- for(int i = 0; i < n; ++i)
- a[i] = new int[n];
- for (int i = 0; i < n; ++i)
- for (int j = 0; j < n; ++j)
- cin >> a[i][j];
- return a;
- }
- void sort(int* a, int n)
- {
- for (int i = 0; i < n; ++i)
- {
- int min = a[i];
- int idx = i;
- for (int j = i; j < n; ++j)
- if (a[j] < min)
- {
- min = a[j];
- idx = j;
- }
- swap(a[i], a[idx]);
- }
- }
- void Replace(int** a, const int* arr, int k, int n) {
- int i, j, ptr = 0;
- if (k < n) {
- i = k;
- j = 0;
- }
- else {
- i = n - 1;
- j = k - n + 1;
- }
- while (i >= 0 && j < n)
- a[i--][j++] = arr[ptr++];
- }
- int main()
- {
- int n;
- cin >> n;
- int** a = init(n);
- for (int k = 0; k < 2*n - 1; ++k)
- {
- int* temp = Get_Line(a, k, n);
- int SIZE;
- if (k < n)
- SIZE = k + 1;
- else
- SIZE = 2 * n - k - 1;
- sort(temp, SIZE);
- Replace(a, temp, k, n);
- }
- cout << endl;
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- cout << a[i][j] << ' ';
- cout << '\n';
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement