Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdio.h>
- #include <cstdlib>
- #include <fstream>
- #include <cctype>
- #include <cassert>
- using namespace std;
- bool DEBUG = !true;
- int** init(int n)
- {
- int c = 0;
- int** a = new int*[n];
- for(int i = 0; i < n; i++) a[i] = new int[n];
- for(int x = 0; x < n; x++)
- {
- for(int y = 0; y < n; y++)
- {
- if((c++) % 2)
- {
- a[x][y] = x + y;
- }
- else
- {
- a[x][y] = x - y;
- }
- if(a[x][y] < 0) a[x][y] = -a[x][y];
- a[x][y] %= 10;
- }
- }
- return a;
- }
- void write(int** a, int n)
- {
- cout << "--- --- ---\n";
- for(int y = 0; y < n; y++)
- {
- for(int x = 0; x < n; x++)
- {
- cout << a[x][y] << " ";
- }
- cout << "\n";
- }
- cout << "--- --- ---\n";
- }
- void write_array(int* a, int n)
- {
- for(int i = 0; i < n; i++) cout << a[i] << " ";
- }
- void dsort(int* a, int n, bool (*dcmp)(int, int))
- {
- int t, li;
- for(int i = 0; i < n - 1; i++)
- {
- li = i;
- for(int j = i + 1; j < n; j++)
- {
- if(dcmp(a[j], a[li])) li = j;
- }
- t = a[i];
- a[i] = a[li];
- a[li] = t;
- }
- }
- bool f_mr_s(int f, int s)
- {
- return f <= s;
- }
- bool f_br_s(int f, int s)
- {
- return f >= s;
- }
- void black_magick(int** a, int n)
- {
- int* ta;
- for(int i = 1; i < n; i++)
- {
- ta = new int[n - i];
- for(int t = 0; t + i < n; t++)
- {
- ta[t] = a[t + i][t];
- }
- if(DEBUG)
- {
- cout << "DEBUG default ---\n";
- write_array(ta, n - i);
- cout << "\n--- --- ---\n";
- }
- dsort(ta, n - i, f_br_s);
- if(DEBUG)
- {
- cout << "DEBUG sort ---\n";
- write_array(ta, n - i);
- cout << "\n--- --- ---\n";
- }
- for(int t = 0; t + i < n; t++)
- {
- a[t + i][t] = ta[t];
- }
- delete[] ta;
- }
- for(int i = 1; i < n; i++)
- {
- ta = new int[n - i];
- for(int t = 0; t + i < n; t++)
- {
- ta[t] = a[t][t + i];
- }
- if(DEBUG)
- {
- cout << "DEBUG default ---\n";
- write_array(ta, n - i);
- cout << "\n--- --- ---\n";
- }
- dsort(ta, n - i, f_mr_s);
- if(DEBUG)
- {
- cout << "DEBUG sort ---\n";
- write_array(ta, n - i);
- cout << "\n--- --- ---\n";
- }
- for(int t = 0; t + i < n; t++)
- {
- a[t][t + i] = ta[t];
- }
- delete[] ta;
- }
- }
- int main()
- {
- //ifstream in("input.txt"); ofstream out("output.txt");
- const int n = 10;
- int** a = init(n);
- write(a, n);
- black_magick(a, n);
- write(a, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement