Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //НЕ РАБОТАЕТ!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- ifstream in("input.txt");
- ofstream out("output.txt");
- void sort(int * a, int n) {
- for (int i = 1; i < n; ++i)
- for (int j = i; j > 0 && a[j - 1] > a[j]; j--)
- swap(a[j], a[j - 1]);
- }
- int* Get_Line(int** a, int k, int n)
- {
- int SIZE, i, j, ptr = 0;
- if (k < n) {
- SIZE = k + 1;
- i = n - k - 1;
- j = 0;
- }
- else {
- SIZE = 2 * n - 1 - k;
- i = 0;
- j = k - n + 1;
- }
- int* res = new int[SIZE];
- while (i > 0 && j < n)
- res[ptr++] = a[i--][j++];
- return res;
- }
- void Replace(int** a, const int* arr, int k, int n)
- {
- int i, j, ptr = 0;
- if (k < n) {
- i = n - k - 1;
- j = 0;
- }
- else {
- i = 0;
- j = k - n + 1;
- }
- while (i > 0 && j < n)
- a[i--][j++] = arr[ptr++];
- }
- int main()
- {
- int n;
- in >> 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)
- in >> a[i][j];
- in.close();
- for (int k = 0; k < 2 * n - 1; ++k) {
- int * cur = Get_Line(a, k, n);//ôóíêöèÿ âîçâðàùàåò k-þ äèàãîíàëü ìàòðèöû a
- int SIZE;//ðàçìåð k-é äèàãîíàëè
- if (k < n)
- SIZE = k + 1;
- else
- SIZE = 2 * n - k - 1;
- sort(cur, SIZE);//ñîðòèðîâêà
- Replace(a, cur, k, n);//çàìåíÿåò â ìàòðèöå a k-äèàãîíàëü ìàññèâîì cur
- delete[] cur;
- }
- for (int i = 0; i < n; ++i, out << '\n')
- for (int j = 0; j < n; ++j, out << ' ')
- out << a[i][j];
- out.close();
- /*
- ïðèìåð âõîäíîãî ôàéëà
- 3
- 9 2 3
- 8 1 6
- 7 4 5
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement