Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h> // в ней функция time
- #include <stdlib.h>
- #define MAX 64
- using namespace std;
- void print_line(int count) { // Разделительная линия
- cout << "--";
- int p = 1;
- while (p < count) {
- p++;
- cout << "---";
- }
- cout << endl;
- }
- void enter_matrix_random(int (&mas)[MAX][MAX], int &N) { // Ввод матрицы рандомом
- srand(time(NULL)); // Рандом рандома
- cout << "Введите разрядность матрицы = ";
- cin >> N;
- print_line(N);
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- mas[i][j] = 10 + rand() % 90; // Каждый элемент случайному числу от 10 до 99
- }
- }
- }
- void enter_matrix_hand(int (&mas)[MAX][MAX], int &N) { // Ввод матрицы от руки
- cout << "Введите разрядность матрицы = ";
- cin >> N;
- print_line(N);
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- cout << "Введите элемент массива (двузначное число) [" << i << "][" << j << "] = ";
- cin >> mas[i][j];
- cout << endl;
- }
- }
- }
- void choice(int (&mas)[MAX][MAX], int &N) { // Выбор способа ввода матрицы
- cout << "Заполнить матрицу случайными числами? [y/n] : ";
- string y = "y";
- string n = "n";
- string c;
- cin >> c;
- if (c == y) {
- cout << "Заполнение случайными числами: " << endl;
- enter_matrix_random(mas, N);
- }
- else if (c == n) {
- cout << "Заполнение от руки:" << endl;
- enter_matrix_hand(mas, N);
- }
- else
- cout << "Ошибка!!!" << endl;
- }
- void print_matrix (int mas[MAX][MAX], int N, string message) { // Печать матрицы
- cout << message << endl;
- print_line(N);
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- cout << mas[i][j] << " ";
- }
- cout << endl;
- }
- print_line(N);
- }
- void quicksort (int a[MAX], int left, int right) { // Быстрая сортировка
- int l = left, r = right;
- int swap;
- int x = a[(left+right) / 2];
- do {
- while (a[l] < x) l++;
- while (a[r] > x) r--;
- if (l <= r) {
- if (a[l] > a[r]) {
- swap = a[l];
- a[l] = a[r];
- a[r] = swap;
- }
- l++; r--;
- }
- }
- while (l <= r);
- if (l < right) quicksort(a, l, right);
- if (left < r) quicksort(a, left, r);
- }
- void changing_matrix(int (&a)[MAX], int mas[MAX][MAX], int &count, int N) { // Чтение элементов массива
- for (int j=0; j<N; j++) {
- int i=0;
- a[count] = mas[i][j];
- count++;
- }
- for (int i=1; i<N-1; i++) {
- int j=N-1;
- a[count] = mas[i][j];
- count++;
- }
- for (int j=N-1; j>0; j--) {
- int i=N-1;
- a[count] = mas[i][j];
- count++;
- }
- for (int i=N-1; i>0; i--) {
- int j=0;
- a[count] = mas[i][j];
- count++;
- }
- /*cout << "Элементы сортировки: ";
- for (int i = 0; i < count; i++) {
- cout << a[i] << " ";
- }
- cout << endl;*/
- quicksort(a, 0, count-1);
- /*cout << "Элементы после сортировки: ";
- for (int i = 0; i < count; i++) {
- cout << a[i] << " ";
- }
- cout << endl;*/
- count = 0;
- for (int j=0; j<N; j++) {
- int i=0;
- mas[i][j] = a[count];
- count++;
- }
- for (int i=1; i<N-1; i++) {
- int j=N-1;
- mas[i][j] = a[count];
- count++;
- }
- for (int j=N-1; j>0; j--) {
- int i=N-1;
- mas[i][j] = a[count];
- count++;
- }
- for (int i=N-1; i>0; i--) {
- int j=0;
- mas[i][j] = a[count];
- count++;
- }
- }
- void finding_doubles(int mas[MAX][MAX], int N) {
- for (int j=0; j<N; j++) {
- for (int i=0; i<N; i++) {
- for (int k=i+1; k<N; k++) {
- if (mas[i][j] == mas [k][j])
- cout << "Номер столбца с повторениями: " << j+1 << " (повторение = " << mas [i][j] << ")" << endl;
- }
- }
- }
- }
- int main() {
- int matrix[MAX][MAX]; // Матрица
- int n; // Разрядность матрицы
- choice(matrix, n);
- print_matrix(matrix, n, "Исходная матрица: ");
- int reqmas[MAX]; // Массив элементов сортировки
- int reqcount = 0; // Количество элементов сортировки
- changing_matrix(reqmas, matrix, reqcount, n);
- print_matrix(matrix, n, "Матрица после сортировки: ");
- finding_doubles(matrix, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement